我试图编写一个Python代码,允许我接收文本,并读取它 按行。在每一行中,单词作为键进入字典,数字应该是指定的值,作为列表。 例如,该文件将由数百行具有相同格式的行组成:
彼得17 29 24 284 72
理想情况下,名称"彼得"将是字典中的一个关键字,值为dict[Peter]: [17, 19, 24, 284,7273]
。
到目前为止我的问题是添加数字。我不确定如何将它们分配给键值。
def wordDict(filename):
inFile=open(filename, 'r')
line=inFile.readline()
while line:
txtWords = line.split() # splits at white space
wordScores={} # make dict
scoreList=[]
for word in txtWords:
word.lower() # turns word into lowercase
if word in string.ascii_lowercase: #if word is alphabetical
if word not in wordScores.keys():
wordScores=wordScores[word] # add the key to dictionary
----------我只有
答案 0 :(得分:1)
使用Python 3.2:
with open("d://test.txt", "r") as fi: # Data read from a text file is a string
d = {}
for i in fi.readlines():
# So you split the line into a list
temp = i.split()
# So, temp = ['Peter', '17', '29', '24', '284', '72']
# You could split 'temp' like so:
# temp[0] would resolve to 'Peter'
# temp[1] would resolve to ['17', '29', '24', '284', '72']
name, num = temp[0], temp[1:]
# From there, you could make temp[0] the key and temp[1:] the value.
# But: notice that the numbers are still represented as strings.
# So, we use the built-in function map() to turn them into integers.
d[name] = [map(int, num)]
答案 1 :(得分:0)
如果你的所有行都以一个单词开头,然后是空格分隔的整数,你可以这样做(未经测试):
myDict = {}
with open('inFile.txt','r') as inFile:
for line in inFile:
line = line.split()
name = line[0].lower()
if name not in myDict:
myDict[name] = map(int,line[1:])
答案 2 :(得分:0)
我将重点介绍代码的两个主要问题:
for word in string.ascii_lowercase:
与撰写for 'hello' in ['a','b,'c']:
相同,它不符合您的期望;并且循环永远不会运行。
wordScores = wordScores[word]
这不会向密钥添加任何内容,您可能需要wordScores[word] = []
。
请改为尝试:
from collections import defauldict
words = defaultdict(list)
with open('somefile.txt') as f:
for line in f:
if line.strip():
bits = line.split()
if bits[0].isalpha():
words[bits[0].lower()] += bits[1:]