pd.read_table字符串解析错误

时间:2018-07-08 07:51:40

标签: python pandas parsing dataframe

解析字符串时需要IO错误帮助,我正在尝试使用Python将多行(字符串可变)写入Pandas数据框。

我的字符串FilteredText包含: u'\ nStrval1 294.25 4.10 1.41 290.15 2,589 7.62 7,043.65 305.70 230.55 \ nStrval2 1,059.00 10.85 1.04 1,048.15 1,676 17.75 9,624.39 1,319.95 915.20 .. [后接大数据行]

我正在尝试使用read_table(也尝试过read_csv) df = pd.read_table(FilteredText,delim_whitespace = True,名称= [“ COL1”,“ COL2”,“ COL3”,..])

File "C:\Python27\lib\site-packages\pandas\io\parsers.py", line 678, in parser_f
return _read(filepath_or_buffer, kwds)

File "C:\Python27\lib\site-packages\pandas\io\parsers.py", line 440, in _read

parser = TextFileReader(filepath_or_buffer, kwds)
File "C:\Python27\lib\site-packages\pandas\io\parsers.py", line 787, in __init__
self._make_engine(self.engine)
File "C:\Python27\lib\site-packages\pandas\io\parsers.py", line 1014, in _make_engine
self._engine = CParserWrapper(self.f, self.options)
File "C:\Python27\lib\site-packages\pandas\io\parsers.py", line 1708, in __init__
self._reader = parsers.TextReader(src, kwds)
File "pandas\_libs\parsers.pyx", line 384, in pandas._libs.parsers.TextReader.__cinit__
File "pandas\_libs\parsers.pyx", line 695, in pandas._libs.parsers.TextReader._setup_parser_source <br/>
**IOError: File**

我也尝试过使用read_csv
df = pd.read_csv(io.StringIO(FilteredText),delim_whitespace = True,

TIA。

2 个答案:

答案 0 :(得分:0)

对我来说,您最后的解决方案有效:

FilteredText=u'\nStrval1 294.25 4.10 1.41 290.15 2,589 7.62 7,043.65 305.70 230.55\nStrval2 1,059.00 10.85 1.04 1,048.15 1,676 17.75 9,624.39 1,319.95 915.20'
n = list('abcdefghij')
df = pd.read_csv(pd.compat.StringIO(FilteredText), delim_whitespace=True, names=n)
print (df)

         a         b      c     d         e      f      g         h         i  \
0  Strval1    294.25   4.10  1.41    290.15  2,589   7.62  7,043.65    305.70   
1  Strval2  1,059.00  10.85  1.04  1,048.15  1,676  17.75  9,624.39  1,319.95   

        j  
0  230.55  
1  915.20  

答案 1 :(得分:0)

此问题已解决。我发现了另一种解析数据的方法。

感谢jezrael的及时答复。