【问题标题】:unable to turn a simple text file into pandas dataframe无法将简单的文本文件转换为 pandas 数据框
【发布时间】:2017-07-17 19:00:46
【问题描述】:

这是我的文件的样子:

raw_file -->

'Date\tValue\tSeries\tLabel\n07/01/2007\t687392\t31537611\tThis home\n08/01/2007\t750624\t31537611\tThis home\n09/01/2007\t769358\t31537611\tThis home\n10/01/2007\t802014\t31537611\tThis home\n11/01/2007\t815973\t31537611\tThis home\n12/01/2007\t806853\t31537611\tThis home\n01/01/2008\t836318\t31537611\tThis home\n02/01/2008\t856792\t31537611\tThis home\n03/01/2008\t854411\t31537611\tThis home\n04/01/2008\t826354\t31537611\tThis home\n05/01/2008\t789017\t31537611\tThis home\n06/01/2008\t754162\t31537611\tThis home\n07/01/2008\t749522\t31537611\tThis home\n08/01/2008\t757577\t31537611\tThis home\n'

type(raw_file) --> <type 'str'>

由于某种原因,I can't use pd.read_csv(raw_file) 否则我会收到错误消息:

File "pandas\_libs\parsers.pyx", line 710, in pandas._libs.parsers.TextReader._setup_parser_source (pandas\_libs\parsers.c:8873)
IOError: File Date  Value   Series  Label
07/01/2007  687392  31537611    This home
08/01/2007  750624  31537611    This home
does not exist

我能想到的最好的是:

for row in raw_file.split('\n'):
   print(row.split('\t'))

这很慢。有没有更好的办法?

【问题讨论】:

    标签: python pandas dataframe text error-handling


    【解决方案1】:

    为什么不使用csv 模块并将分隔符设置为\t

    https://docs.python.org/3.4/library/csv.html

    使用 csv.reader(your_file, delimiter='\t') 作为 f: # 做事

    【讨论】:

      【解决方案2】:

      当你给 pandas 一个 string 作为 filepath_or_buffer 参数时 - 它认为它是一个文件名或 URL。

      来自docs

      filepath_or_bufferstrpathlib.Pathpy._path.local.LocalPath 或任何具有read() 方法的对象(例如文件handleStringIO

      字符串可以是 URL。有效的 URL 方案包括 http、ftp、s3、 和档案。对于文件 URL,需要一个主机。例如,本地

      文件可以是文件://localhost/path/to/table.csv

      解决方法:使用io.StringIO()构造函数:

      In [69]: pd.read_csv(io.StringIO(raw_file), delim_whitespace=True)
      Out[69]:
                    Date     Value Series Label
      07/01/2007  687392  31537611   This  home
      08/01/2007  750624  31537611   This  home
      09/01/2007  769358  31537611   This  home
      10/01/2007  802014  31537611   This  home
      11/01/2007  815973  31537611   This  home
      12/01/2007  806853  31537611   This  home
      01/01/2008  836318  31537611   This  home
      02/01/2008  856792  31537611   This  home
      03/01/2008  854411  31537611   This  home
      04/01/2008  826354  31537611   This  home
      05/01/2008  789017  31537611   This  home
      06/01/2008  754162  31537611   This  home
      07/01/2008  749522  31537611   This  home
      08/01/2008  757577  31537611   This  home
      

      【讨论】:

        猜你喜欢
        • 2021-03-01
        • 2019-09-25
        • 2020-02-29
        • 2022-01-10
        • 2013-01-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-11
        相关资源
        最近更新 更多