【问题标题】:pd.read_table Error on String Parsingpd.read_table 字符串解析错误
【发布时间】:2018-12-16 05:08:23
【问题描述】:

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

我的字符串 FilteredText 包含: U'\ ntval1 294.25 4.10 1.41 290.15 2,589 7.62 7,043.65 305.70 230.55 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, 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。

【问题讨论】:

    标签: python pandas parsing dataframe


    【解决方案1】:

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

    感谢 jezrael 的及时回复。

    【讨论】:

      【解决方案2】:

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

      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  
      

      【讨论】:

      • 谢谢,但是 read_csv 只处理前 50 行。
      • @suraj_Blore - 为什么你认为只有50 rows 进程,它是如何验证的?也许数据有问题?
      • 我已经检查了那一侧,第 51 行没有什么不同,没有特殊字符。另外,我找不到任何要排除故障的警告。
      • 非常有趣的是,当我尝试使用 "python -m pdb .py 调试它时,它会处理所有的行,真的很棒。
      • 也许问题你的输出只显示第一行,如果检查print (df.tail(20))它返回最后一行?
      猜你喜欢
      • 1970-01-01
      • 2013-05-10
      • 1970-01-01
      • 2014-08-26
      • 2012-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多