简体   繁体   English

Python 逐行读取文件并转换为字典

[英]Python read file line by line and convert to dictionary

I have below data in file.sec:我在 file.sec 中有以下数据:

goog,100
goog,101
goog,103
micro,200
micro,201
face,99

I want to convert this data into a dictionary: {'goog': [100,101,103], 'micro': [200, 201], 'face': [99]}我想将此数据转换为字典: {'goog': [100,101,103], 'micro': [200, 201], 'face': [99]}

I tried below code, but it wipes out the list whenever value changes from goog to micro and result I get is: {'goog': [99], 'micro': [99], 'face':[99]}我尝试了下面的代码,但是每当值从 goog 更改为 micro 时它就会清除列表,我得到的结果是:{'goog': [99], 'micro': [99], 'face':[99]}

allD = {}
allN = []
f = open('file.sec' , 'r')
for data in f:
   com = data.split(',')[0]
   
   if com not in allD.keys():
      del allN[:]

   allN.append( data.split(',')[1] )

   allD[ com ] = allN  
    
 print allD        

You could use defaultdict你可以使用defaultdict

from collections import defaultdict

result = defaultdict(list)

f = open('file.sec' , 'r')
for data in f:
    com = data.split(',')[0]
    result[com].append(data.split(',')[1]

You could use a defaultdict to solve this:您可以使用 defaultdict 来解决此问题:

from collections import defaultdict

data_dict = defaultdict(list)

f = open('file.sec' , 'r')
for data in f:
    key, value = data.split(',')
    data_dict[key].append(value)

Note that i've written this in python 3 syntax, so you may need to do it slightly differently for python 2.请注意,我是用 python 3 语法编写的,因此您可能需要对 python 2 稍作不同的处理。

You are not far, but there is a misunderstanding on what a Python assignment is.您不远,但对 Python 分配是什么存在误解。 When you write allD[ com ] = allN , allD[com] is not a copy of allN but is just another reference to the very same object.当您编写allD[ com ] = allN时, allD[com]不是allN的副本,而只是对相同 object 的另一个引用。 So when you later clean allN with del allN[:] you actually clean the last allD[com] .因此,当您稍后使用del allN[:]清理allN时,您实际上清理了最后一个allD[com] You should instead use a new object:您应该改用新的 object:

allD = {}
allN = []
f = open('file.sec' , 'r')
for data in f:
   com = data.split(',')[0]
   
   if com not in allD.keys():
      allN = []
      allD[ com ] = allN  

   allN.append( data.split(',')[1] )

    
 print allD

An example without defaultdict:一个没有 defaultdict 的例子:

allD = {}
allN = []
f = open('data.csv', 'r')
for data in f:
    com = data.split(',')[0]

    if com not in allD.keys():
        allD[com] = []

    allD[com].append(data.split(',')[1].strip())

print(allD)

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

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