简体   繁体   中英

Python read comma separated txt file having thousands separator

My comma separated txt file contains data in the following format (of course, there are more columns and rows).

1977,PA,2017-08-07,55,New Firms,327.0
1978,NY,2017-08-07,45,New Firms,$30,127
1978,NY,2017-08-07,$10,000,New Firms,1,000

As you can see, some data contain thousands separator and I read like this:

df=pd.read_csv("data.txt", thousands=r',')
df.head()

This gives an error message

ParserError: Error tokenizing data. C error: Expected 13 fields in line 102996, saw 14

I think the error occurs because some data contain thousands separators.

Any suggestions?

Try with this

df=pd.read_csv("text.csv", sep=', ',header =None,names=['colA','colB','colC','colD','colE','colF'])

df.head()

it outputs:

Out[25]: 
   colA colB        colC     colD       colE     colF
0  1977   PA  2017-08-07       55  New Firms    327.0
1  1978   NY  2017-08-07       45  New Firms  $30,127
2  1978   NY  2017-08-07  $10,000  New Firms    1,000

也许还有一个额外的sep参数:

df = pd.read_csv("data.txt", sep = ', ', header = None, thousands = ',')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM