简体   繁体   English

python-列表以字符串形式从csv中读取

[英]python - lists are being read in from csv as strings

I have a dictionary that uses an strings(edit) as keys and stores lists of lists as values. 我有一本字典,它使用字符串(编辑)作为键并将列表列表存储为值。

dict = {key1: [[data1],[data2],[data3]], key2: [[data4],[data5]],...etc}

EDIT: where the data variables are rows containing different data types from a converted pandas DataFrame 编辑:数据变量是包含从转换后的熊猫DataFrame不同数据类型的行

Ex. 防爆。

df = pd.DataFrame()
df['City'] = ['New York','Austin','New Orleans','New Orleans']
df['State'] = ['NY','TX','LA','LA']
df['Latitude'] = [29.12,23.53,34.53,34.53]
df['Time'] = [1.46420e+09,1.47340e+09,1.487820e+09,1.497820e+09]

City         State    Latitude   Time
New York     NY       29.12      1.46420e+09
Austin       TX       23.53      1.47340e+09
New Orleans  LA       34.53      1.487820e+09
New Orleans  LA       34.53      1.497820e+09

dict = {}
cities = df['City'].unique()
for c in cities:
    temp = df[df['City'] == c]
    dict[c] = temp.as_matrix().tolist()

#which outputs this for a given key
dict['New Orleans'] = [['New Orleans' 'LA' 34.53  1.487820e+09],
    ['New Orleans' 'LA' 34.53  1.497820e+09]]

I am storing it as a csv using the following: 我使用以下命令将其存储为csv:

filename = 'storage.csv'
with open(filename,'w') as f:
    w = csv.writer(f)
    for key in dict.keys():
        w.writerow((key,dict[key]))

I then read the file back into a dictionary using the following: 然后,我使用以下命令将文件读回到字典中:

reader = csv.reader(open(filename, 'r'))
dict = {}
for key,val in reader:
    dict[key] = val

val comes in looking perfect, except it is now a string. val看起来很完美,只是现在它是一个字符串。 for example, key1 looks like this: 例如,key1看起来像这样:

dict[key1] = "[[data1],[data2],[data3]]"

How can I read the values in as lists, or remove the quotes from the read-in version of val? 如何读取as列表中的值,或从val的读取版本中删除引号?

Edit: Since you are using a pandas.DataFrame don't use the csv module or json module. 编辑:由于您使用的是pandas.DataFrame请不要使用csv模块或json模块。 Instead, use pandas.io for both reading and writing. 而是使用pandas.io进行读写。


Original Answer: 原始答案:

Short answer: use json . 简短的答案:使用json

CSV is fine for saving tables of strings. CSV适用于保存字符串表。 Anything further than that and you need to manually convert the strings back into Python objects. 除此之外,您还需要手动将字符串转换回Python对象。

If your data has just lists, dictionaries and basic literals like strings and numbers json would be the right tool for this job. 如果您的数据只有列表,那么字典和基本文字(如字符串和数字json将是此工作的正确工具。

Given: 鉴于:

example = {'x': [1, 2], 'y': [3, 4]}

Save to file: 保存到文件:

with open('f.txt','w') as f:
    json.dump(example, f)

Load from file: 从文件加载:

with open('f.txt') as f:
    reloaded_example = json.load(f)

your code must be like : 您的代码必须类似于:

import csv
import ast
#dict = {1: [[1],[2],[3]], 2: [[4],[5]]}
reader = csv.reader(open("storage.csv", 'r'))
dict = {}
for key,val in reader:
    dict[int(key)] = ast.literal_eval(val)
print dict

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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