Cameron
Cameron

Reputation: 13

Python pandas read_csv with " delimiter

I am trying to read in a csv file of stock data which is set out as follows:

        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

etc

The delimiter is clearly " but when I run the below code it just reads it in as one column and includes the " in the data.

import pandas as pd

stock1 = 'CS.csv'

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

Any help would be appreciated.

Upvotes: 0

Views: 6638

Answers (1)

cs95
cs95

Reputation: 402313

The python parser can parse CSVs with complex regex patterns as the delimiter.

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

Upvotes: 2

Related Questions