Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to read comma seperated values from excel and store them in a list?

I’m trying to read some comma separated values[string values] from a particular column in excel. I want to store these values as they are in a list in python.

This is the data in that column : ‘a’,’b c’,’d’,’f,g’

When i try reading the same from excel to python, it is read as a string :
This is what it looks like : "’a’,’b c’,’d’,’f,g’" giving me a complete string.
I cannot split it based on ‘,’ since my data can have ‘,’ within it.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I just want [‘a’,’b c’,’d’,’f,g’], how do i get this ?

>Solution :

IIUC, you can try this :

df = pd.read_excel("file.xlsx")
​
df["col"] = df["col"].str.strip("'").str.split("','")

Output :

print(df.iloc[0,0])
#['a', 'b c', 'd', 'f,g']

print(df)
                col
0  [a, b c, d, f,g]

Input used :

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading