1

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

my string FilteredText contains: 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.. [followed by BULK LINES of DATA]

我正在尝试使用 read_table (也尝试过 read_csv) df = pd.read_table(FilteredText, delim_whitespace=True, names = ["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。

4

2 回答 2

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  
于 2018-07-08T07:56:14.193 回答
0

这已解决。我找到了另一种解析数据的方法。

感谢 jezrael 的及时回复。

于 2018-07-08T14:04:09.673 回答