Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python pandas read_csv quotechar does not work

Tags:

python

pandas

csv

I've read this, this and this posts but despite I don't know why quotechar does not work at pd.read_csv() (Python 3, pandas 0.18.0 and 0.18.1). And how could I read a dataframe like this:

"column1","column2", "column3", "column4", "column5", "column6"
"AM", 7, "1", "SD", "SD", "CR"
"AM", 8, "1,2 ,3", "PR, SD,SD", "PR ; , SD,SD", "PR , ,, SD ,SD"
"AM", 1, "2", "SD", "SD", "SD"

I want the following result:

Out[116]: 
  column1  column2 column3    column4       column5        column6
0      AM        7       1         SD            SD             CR
1      AM        8  1,2 ,3  PR, SD,SD  PR ; , SD,SD  PR , ,, SD,SD
2      AM        1       2         SD            SD             SD

Thank you!!

like image 780
ragesz Avatar asked May 06 '16 14:05

ragesz


People also ask

How do I read a .data file in pandas?

We can read data from a text file using read_table() in pandas. This function reads a general delimited file to a DataFrame object. This function is essentially the same as the read_csv() function but with the delimiter = '\t', instead of a comma by default.

How can you get the first five records in a DataFrame?

You can use df. head() to get the first N rows in Pandas DataFrame.

How do I read a CSV file without a column name in python?

To read CSV file without header, use the header parameter and set it to “None” in the read_csv() method.


1 Answers

Pandas doc on separators in read_csv():

Separators longer than 1 character and different from '\s+' will be interpreted as regular expressions, will force use of the python parsing engine and will ignore quotes in the data.

Try using this instead (sep by default set to a comma):

pd.read_csv(file, skipinitialspace = True, quotechar = '"')
like image 177
ptrj Avatar answered Nov 04 '22 16:11

ptrj