用可变间距读取python中的文本文件

时间:2017-05-05 18:06:44

标签: python python-3.x csv pandas

我有一个文本文件形式的以下数据,我想加载到python:

      pclass  survived                                               name  
0          1         1                      Allen, Miss. Elisabeth Walton   
1          1         1                     Allison, Master. Hudson Trevor   
2          1         0                       Allison, Miss. Helen Loraine   
3          1         0               Allison, Mr. Hudson Joshua Creighton   
4          1         0    Allison, Mrs. Hudson J C (Bessie Waldo Daniels)   
5          1         1                                Anderson, Mr. Harry   
6          1         1                  Andrews, Miss. Kornelia Theodosia   
7          1         0                             Andrews, Mr. Thomas Jr   
8          1         1      Appleton, Mrs. Edward Dale (Charlotte Lamson)   
9          1         0                            Artagaveytia, Mr. Ramon   
10         1         0                             Astor, Col. John Jacob   

由于空格不是常量,并且由于最后一个字段(名称)之间有空格,因此我无法解析它。我尝试了以下方法:

pd.read_csv("test.csv",sep = "\s+", header=0, index_col=0)

但是它给出了一个错误:

CParserError: Error tokenizing data. C error: Expected 7 fields in line 5, saw 8

2 个答案:

答案 0 :(得分:3)

'\s+'假设一个或多个空格仍在解析您的最终列。而是使用假定两个或更多的正则表达式。

pd.read_csv("test.csv", sep="\s{2,}", header=0, index_col=0, engine='python')

整个工作示例

from io import StringIO
import pandas as pd

txt = """     pclass  survived                                               name  
0          1         1                      Allen, Miss. Elisabeth Walton   
1          1         1                     Allison, Master. Hudson Trevor   
2          1         0                       Allison, Miss. Helen Loraine   
3          1         0               Allison, Mr. Hudson Joshua Creighton   
4          1         0    Allison, Mrs. Hudson J C (Bessie Waldo Daniels)   
5          1         1                                Anderson, Mr. Harry   
6          1         1                  Andrews, Miss. Kornelia Theodosia   
7          1         0                             Andrews, Mr. Thomas Jr   
8          1         1      Appleton, Mrs. Edward Dale (Charlotte Lamson)   
9          1         0                            Artagaveytia, Mr. Ramon   
10         1         0                             Astor, Col. John Jacob   
"""

pd.read_csv(StringIO(txt), sep="\s{2,}", header=0, index_col=0, engine='python')

    pclass  survived                                             name
0        1         1                    Allen, Miss. Elisabeth Walton
1        1         1                   Allison, Master. Hudson Trevor
2        1         0                     Allison, Miss. Helen Loraine
3        1         0             Allison, Mr. Hudson Joshua Creighton
4        1         0  Allison, Mrs. Hudson J C (Bessie Waldo Daniels)
5        1         1                              Anderson, Mr. Harry
6        1         1                Andrews, Miss. Kornelia Theodosia
7        1         0                           Andrews, Mr. Thomas Jr
8        1         1    Appleton, Mrs. Edward Dale (Charlotte Lamson)
9        1         0                          Artagaveytia, Mr. Ramon
10       1         0                           Astor, Col. John Jacob

答案 1 :(得分:2)

您可以使用pandas.read_fwf(又名:固定宽度格式)来执行此操作:

<强>代码:

df = pd.read_fwf(StringIO(data), header=1, index_col=0)

测试代码:

from io import StringIO
import pandas as pd

data = u"""
      pclass  survived                                               name
0          1         1                      Allen, Miss. Elisabeth Walton
1          1         1                     Allison, Master. Hudson Trevor
2          1         0                       Allison, Miss. Helen Loraine
3          1         0               Allison, Mr. Hudson Joshua Creighton
4          1         0    Allison, Mrs. Hudson J C (Bessie Waldo Daniels)
5          1         1                                Anderson, Mr. Harry
6          1         1                  Andrews, Miss. Kornelia Theodosia
7          1         0                             Andrews, Mr. Thomas Jr
8          1         1      Appleton, Mrs. Edward Dale (Charlotte Lamson)
9          1         0                            Artagaveytia, Mr. Ramon
10         1         0                             Astor, Col. John Jacob"""

df = pd.read_fwf(StringIO(data), header=1, index_col=0)
print(df)

<强>结果:

    pclass  survived                                             name
0        1         1                    Allen, Miss. Elisabeth Walton
1        1         1                   Allison, Master. Hudson Trevor
2        1         0                     Allison, Miss. Helen Loraine
3        1         0             Allison, Mr. Hudson Joshua Creighton
4        1         0  Allison, Mrs. Hudson J C (Bessie Waldo Daniels)
5        1         1                              Anderson, Mr. Harry
6        1         1                Andrews, Miss. Kornelia Theodosia
7        1         0                           Andrews, Mr. Thomas Jr
8        1         1    Appleton, Mrs. Edward Dale (Charlotte Lamson)
9        1         0                          Artagaveytia, Mr. Ramon
10       1         0                           Astor, Col. John Jacob