2

I've recently started to use Pandas.

Here's my csv file.

column1,column2,column3 a, b, c a, b, "c, d" 

I want "c, d" to be in column3 like here:

Column1 Column2 Column3
a b c
a b c, d

But using data = pd.read_csv('testfile.csv', sep=',', quotechar='"', encoding='utf-8') I get this table instead:

Column1 Column2 Column3
a b c
a, b, "c, d" None None

I've tried to change values of some of parameters in read_csv. And also regular expression from here.

1 Answer 1

7

You might try

data = pd.read_csv('testfile.csv', sep=',', quotechar='"', skipinitialspace=True, encoding='utf-8') 

which tells pandas to ignore the space that comes after the comma, otherwise it can't recognize the quote.

EDIT: Apparently this does not work for the author of the question

Therefore, this is a script that produces the wanted result. I have python 3.8.9, pandas 1.2.3.

itworks.py

import pandas as pd with open("testfile.csv", "w") as f: f.write("""column1,column2,column3 a, b, c a, c, "c, d" """) data = pd.read_csv("testfile.csv", sep=",", quotechar='"', skipinitialspace=True, encoding="utf-8") print(data) 
$ python itworks.py column1 column2 column3 0 a b c 1 a c c, d $ 

Try to reproduce this minimal example.

Sign up to request clarification or add additional context in comments.

5 Comments

I've tried this before asking ) It didn't help.
@Ferapont I tried this on my computer, with exactly your input and the exact same command I showed in the answer, and I get exactly the result you want.
This script works on my PC too. But when I edit csv in excel, save it and after that call pd.read_csv("testfile.csv", ...) I get the same wrong result. I'm now trying to understand why it works in such way.
Here is the explanation.
Well, I guess you could also try not to use Microsoft Excel. There are alternatives which are more programming-friendly, and more powerful (but less user-friendly).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.