带有分隔符的Python熊猫read_csv

时间:2018-09-30 02:22:15

标签: python pandas delimiter

我正在尝试读取库存数据的csv文件,其格式如下:

        Date           "Open           "High           "Low            "Close          "Volume         "Open Interest  "Ticker

  1999-1-21"       33.07198"       33.60028"       32.96632"       33.07198"          36201"             39"             CS
  1999-1-22"       32.01537"       32.22669"       32.01537"       32.01537"           3667"             38"             CS
  1999-1-25"       32.12103"       32.75499"       32.12103"       32.12103"           2366"             38"             CS
  1999-1-26"       32.01537"       32.01537"       32.01537"       32.01537"          14315"             38"             CS

定界符显然是“,但是当我运行以下代码时,它只是将其读入一列并在数据中包含”。

import pandas as pd

stock1 = 'CS.csv'

x = pd.read_csv(stock1, delimiter='"')

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

python解析器可以解析具有复杂正则表达式模式的CSV作为定界符。

df = pd.read_csv(filename, sep=r'\s*"\s*', engine='python')
print(df)
        Date      Open      High       Low     Close  Volume  Open Interest Ticker
0  1999-1-21  33.07198  33.60028  32.96632  33.07198   36201             39     CS
1  1999-1-22  32.01537  32.22669  32.01537  32.01537    3667             38     CS
2  1999-1-25  32.12103  32.75499  32.12103  32.12103    2366             38     CS
3  1999-1-26  32.01537  32.01537  32.01537  32.01537   14315             38     CS