简体   繁体   中英

Python pandas read list in list data type from csv file

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.

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})

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM