[英]Reading from a text file into a dictionary with variable columns
I'm currently learning about data mining and one of the examples in the ebook I'm reading had a dictionary to store every user and their ratings for a song. 我目前正在学习有关数据挖掘的知识,正在阅读的电子书中的示例之一中有一个字典来存储每个用户及其歌曲的评分。 This is the initialization of the dictionary that was given. 这是给出的字典的初始化。
users ={"Angelica": {"Blues Traveler": 3.5, "Broken Bells": 2.0,
"Norah Jones": 4.5, "Phoenix": 5.0,
"Slightly Stoopid": 1.5,
"The Strokes": 2.5, "Vampire Weekend": 2.0},
"Bill": {"Blues Traveler": 2.0, "Broken Bells": 3.5,
"Deadmau5": 4.0, "Phoenix": 2.0,
"Slightly Stoopid": 3.5, "Vampire Weekend": 3.0},
"Chan": {"Blues Traveler": 5.0, "Broken Bells": 1.0,
"Deadmau5": 1.0, "Norah Jones": 3.0,
"Phoenix": 5, "Slightly Stoopid": 1.0}}
I'm stuck on figuring out how to create this same dictionary if the same values were in a text file where each line contains the information of each user. 如果文本文件中的相同值(每行包含每个用户的信息)中的值相同,我将继续研究如何创建此字典。 This would be an example of the first line in the text file: 这将是文本文件中第一行的示例:
Angelica, "Blues Traveler": 3.5, "Broken Bells": 2.0, "Norah Jones": 4.5, "Phoenix": 5.0, "Slightly Stoopid": 1.5, "The Strokes": 2.5, "Vampire Weekend": 2.0
What I have so far: 到目前为止,我有:
with open(text_file) as f:
for line in f:
songs = line.split(',')
for current_song in songs
ratings = current_songs.split(':')
I'm not too sure how create the dictionary. 我不太确定如何创建字典。 The nested dictionaries have been confusing me for a few hours. 嵌套的字典使我困惑了几个小时。
users = {}
with open(text_file) as f:
for line in f:
parts = line.rstrip().split(', ')
name = parts[0]
users[name] = {}
for rating in parts[1:]:
song, score = rating.split(': ')
song = song[1:-1]
users[name][song] = score
print users
Can be made more concise with json library. 可以使用json库更加简洁。 We will do following: 我们将执行以下操作:
First lets split the line and separate artists name and data. 首先,让我们分开界线,并分离艺术家姓名和数据。 So, string Angelica, "Blues Traveler": 3.5, "Broken Bells": 2.0, "Norah Jones": 4.5,...
is split into two strings Angelica
and second string "Blues Traveler": 3.5, "Broken Bells": 2.0, "Norah Jones": 4.5,...
因此, Angelica, "Blues Traveler": 3.5, "Broken Bells": 2.0, "Norah Jones": 4.5,...
字符串Angelica, "Blues Traveler": 3.5, "Broken Bells": 2.0, "Norah Jones": 4.5,...
被拆分为两个字符串Angelica
和第二个字符串"Blues Traveler": 3.5, "Broken Bells": 2.0, "Norah Jones": 4.5,...
username, songs = line.split(',', 1)
If you observe carefully, the second string can be easily converted to a dictionary by importing it into json.loads
, however it does not have {
and }
to make it a valid json. 如果仔细观察,可以通过将第二个字符串导入json.loads
轻松地将其转换为字典,但是它没有{
和}
使其成为有效的json。 So we will add it manually and import it into json. 因此,我们将手动添加它并将其导入json。
songs = "{%s}" % songs json.loads(songs)
So total code is: 因此,总代码为:
import json
user = {}
with open('my.txt') as f:
for line in f:
username, songs = line.split(',', 1)
songs = "{%s}" % songs
user[username] = json.loads(songs)
print user
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.