使用pandas.read_csv导入具有空格的文本数据

时间:2018-12-19 10:12:04

标签: python pandas regex-negation

我想使用pandas.read_csv导入文本文件:

1541783101     8901951488  file.log             12345  123456
1541783401     21872967680  other file.log       23456     123
1541783701     3  third file.log 23456     123

此处的困难在于,列之间用一个或多个空格分隔,但是有一列包含一个带有空格的文件名。因此,我无法使用sep=r"\s+"来标识列,因为在第一个具有空格的文件名时会失败。文件格式没有固定的列宽。

但是,每个文件名都以“ .log”结尾。我可以编写与各列匹配的单独的正则表达式。是否可以使用这些标识要导入的列?还是可以编写一个分隔符正则表达式来选择所有不匹配任何匹配正则表达式的列的字符?

1 个答案:

答案 0 :(得分:2)

回答最新问题-

这里的代码无论数据宽度如何都不会失败。您可以根据需要进行修改。

df = pd.read_table('file.txt', header=None)

# Replacing uneven spaces with single space
df = df[0].apply(lambda x: ' '.join(x.split()))

# An empty dataframe to hold the output
out = pd.DataFrame(np.NaN, index=df.index, columns=['col1', 'col2', 'col3', 'col4', 'col5'])

n_cols = 5      # number of columns
for i in range(n_cols-2):
    # 0 1
    if i == 0 or i == 1:
        out.iloc[:, i] = df.str.partition(' ').iloc[:,0]
        df = df.str.partition(' ').iloc[:,2]
    else:
        out.iloc[:, 4] = df.str.rpartition(' ').iloc[:,2]
        df = df.str.rpartition(' ').iloc[:,0]
        out.iloc[:,3] = df.str.rpartition(' ').iloc[:,2]
        out.iloc[:,2] = df.str.rpartition(' ').iloc[:,0]

print(out)

+---+------------+-------------+----------------+-------+--------+
|   |    col1    |      col2   |       col3     |   col4 |   col5 |
+---+------------+-------------+----------------+-------+--------+
| 0 | 1541783101 |  8901951488 | file.log       | 12345 | 123456 |
| 1 | 1541783401 | 21872967680 | other file.log | 23456 |    123 |
| 2 | 1541783701 |           3 | third file.log | 23456 |    123 |
+---+------------+-------------+----------------+-------+--------+

注意-该代码被硬编码为5列。也可以泛化。

上一个答案-

使用pd.read_fwf()读取固定宽度的文件。

在您的情况下:

pd.read_fwf('file.txt', header=None)

+---+----------+-----+-------------------+-------+--------+
|   |    0     |  1  |         2         |   3   |   4    |
+---+----------+-----+-------------------+-------+--------+
| 0 | 20181201 |   3 | file.log          | 12345 | 123456 |
| 1 | 20181201 |  12 | otherfile.log     | 23456 |    123 |
| 2 | 20181201 | 200 | odd file name.log | 23456 |    123 |
+---+----------+-----+-------------------+-------+--------+