Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

problems dealing with pandas read csv

Tags:

python

pandas

I've got a problem with pandas read_csv. I had a many txt files that associate with stock market.It's like this:

SecCode,SecName,Tdate,Ttime,LastClose,OP,CP,Tq,Tm,Tt,Cq,Cm,Ct,HiP,LoP,SYL1,SYL2,Rf1,Rf2,bs,s5,s4,s3,s2,s1,b1,b2,b3,b4,b5,sv5,sv4,sv3,sv2,sv1,bv1,bv2,bv3,bv4,bv5,bsratio,spd,rpd,depth1,depth2  
600000,浦发银行,20120104,091501,8.490,.000,.000,0,.000,0,0,.000,0,.000,.000,.000,.000,.000,.000, ,.000,.000,.000,.000,8.600,8.600,.000,.000,.000,.000,0,0,0,0,1100,1100,38900,0,0,0,.00,.000,.00,.00,.00
600000,浦发银行,20120104,091506,8.490,.000,.000,0,.000,0,0,.000,0,.000,.000,.000,.000,.000,.000, ,.000,.000,.000,.000,8.520,8.520,.000,.000,.000,.000,0,0,0,0,56795,56795,33605,0,0,0,.00,.000,.00,.00,.00
600000,浦发银行,20120104,091511,8.490,.000,.000,0,.000,0,0,.000,0,.000,.000,.000,.000,.000,.000, ,.000,.000,.000,.000,8.520,8.520,.000,.000,.000,.000,0,0,0,0,56795,56795,34605,0,0,0,.00,.000,.00,.00,.00
600000,浦发银行,20120104,091551,8.490,.000,.000,0,.000,0,0,.000,0,.000,.000,.000,.000,.000,.000, ,.000,.000,.000,.000,8.520,8.520,.000,.000,.000,.000,0,0,0,0,56795,56795,35205,0,0,0,.00,.000,.00,.00,.00
600000,浦发银行,20120104,091621,8.490,.000,.000,0,.000,0,0,.000,0,.000,.000,.000,.000,.000,.000, ,.000,.000,.000,.000,8.520,8.520,.000,.000,.000,.000,0,0,0,0,57795,57795,34205,0,0,0,.00,.000,.00,.00,.00

while I use this code to read it :

fields = ['SecCode', 'Tdate','Ttime','LastClose','OP','CP','Rf1','Rf2']  
df = pd.read_csv('SHL1_TAQ_600000_201201.txt',usecols=fields)

But I got a problem:

Traceback (most recent call last):
  File "E:/workspace/Senti/highlevel/highlevel.py", line 8, in <module>
    df = pd.read_csv('SHL1_TAQ_600000_201201.txt',usecols=fields,header=1)
  File "D:\Anaconda2\lib\site-packages\pandas\io\parsers.py", line 562, in parser_f
    return _read(filepath_or_buffer, kwds)
  File "D:\Anaconda2\lib\site-packages\pandas\io\parsers.py", line 315, in _read
    parser = TextFileReader(filepath_or_buffer, **kwds)
  File "D:\Anaconda2\lib\site-packages\pandas\io\parsers.py", line 645, in __init__
    self._make_engine(self.engine)
  File "D:\Anaconda2\lib\site-packages\pandas\io\parsers.py", line 799, in _make_engine
    self._engine = CParserWrapper(self.f, **self.options)
  File "D:\Anaconda2\lib\site-packages\pandas\io\parsers.py", line 1257, in __init__
    raise ValueError("Usecols do not match names.")
ValueError: Usecols do not match names.

I can't find any problem similar to mine.And also it's wired when I copy the txt file into another one ,the code runs well,but the original one cause the above problem.How can I solve it ?

like image 376
user6007834 Avatar asked Oct 29 '22 18:10

user6007834


1 Answers

In your message, you said that you're a running:

df = pd.read_csv('SHL1_TAQ_600000_201201.txt',usecols=fields)

Which did not throw an error for me and @Anil_M. But from your traceback, it is possible to see that the command used is another one:

df = pd.read_csv('SHL1_TAQ_600000_201201.txt',usecols=fields, header=1)

which includes a header=1 and it throws the error mentioned.

So, I would guess that the error comes from some confusion on your code.

like image 62
gabra Avatar answered Nov 15 '22 05:11

gabra