3

I have a csv file, like this:

A       B
aaa     [['a1','b1'],['a2','b2']]
bbb     [['a3','b3']]

I tried to read this file into python. And I want the 'B' column in list data type.
I tried pd.read_csv('filename.csv'), what I got is "[['a1','b1'],['a2','b2']]" in str type, not in list type.
And I also tried pd.read_csv('filename.csv', converters={'B':list}), what I got here is like: [[, [',a','1,','b,'...., not the list I want.
Could you tell me how to read the list from a csv file directly? Thank you so much.

1
  • 1
    can you share csv file Commented Nov 4, 2017 at 6:44

3 Answers 3

3

The issue here is caused by the serialization format of the list in column B. It is stored as a string representation of the list. In order to recover a python list back from the string, you can use eval as a converter:

df = pd.read_csv(source, converters={'B': eval})
Sign up to request clarification or add additional context in comments.

Comments

0

to read list you should try this

import csv
with open('filename.csv', 'rb') as f:
    reader = csv.reader(f)
    your_list = list(reader)

print your_list

Comments

0

I use pandas to handle file easily. If you can decide what gets in your file, you could also choose to store something like this:

A,B
aaa,a1 b1 + a2 b2
bbb,a3 b3

Then you can simply read your file and use the power of the split function which will:

  • split your string with the seperator of your choice (white space by default)
  • return the result in a list object.

For your second colum of the first row, you can simply run split once with the white space as a seperator and run it again with '+' as a seperator.

You can find more details on split here https://www.w3schools.com/python/ref_string_split.asp

import pandas

df = pandas.read_csv('my_file.csv')
for row in df.itertuples():
  my_list = row.A.split()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.