[英]How to read each line of a file to a separate list to process them individually
There are already several questions to similar topics, but none of them solves mine. 类似主题已经有几个问题了,但是没有一个能解决我的问题。
I've written multiple lists to a text file. 我已经将多个列表写入一个文本文件。 There, every line represents a list. 在那里,每一行代表一个列表。 Looks like this: 看起来像这样:
1: ['4bf58dd8d48988d1ce941735', '4bf58dd8d48988d157941735', '4bf58dd8d48988d1f1931735', etc.]
2: ['4bf58dd8d48988d16a941735', '4bf58dd8d48988d1f6941735', '4bf58dd8d48988d143941735', etc.]
...
I created it with: 我用以下方法创建了它:
with open('user_interest.txt', 'w') as f:
for x in range(1, 1084):
temp = df.get_group(x)
temp_list = temp['CategoryID'].tolist()
f.write(str(temp_list) + "\n")
If I read the file I get the whole file as a list. 如果我读取文件,则会得到整个文件的列表。 If I then access the lines, I have them as class string! 如果然后访问这些行,则将它们作为类字符串! But I want them again as a list like before I stored them. 但是我又想像它们存储之前一样将它们作为列表。
with open('user_interest.txt', 'r') as file:
for line in file:
#temp_list.append(line)
print(similarity_score(user_1_list, temp_list))
line is class string here, not list like I wanted. 行是这里的类字符串,而不是我想要的列表。 The idea with temp_list doesn't really work either. 带有temp_list的想法实际上也不起作用。 (user_1_list is a fix value, while temp_list is not) (user_1_list是一个固定值,而temp_list不是)
Here's the context of the question: I want every line to be processed in my similarity_score function. 这是问题的上下文:我希望每一行都在我的sameity_score函数中进行处理。 I don't need the lists "forever" just hand it over to my function. 我不需要列表“永远”就将其移交给我的职能。 This function should be applied to every line. 此功能应应用于每一行。 The function calculates cosine similarity and I have to find top 10 most similar users to a given user. 该函数计算余弦相似度,我必须找到给定用户的十大最相似用户。 So I have to compare each other user with my given user (user_1_list). 因此,我必须将其他用户与给定的用户(user_1_list)进行比较。
Psedo code: 伪代码:
read line
convert line to a list
give list to my function
read next line ...
Probably it's just an easy fix, but I don't get it yet. 也许这只是一个简单的解决方法,但我还没有得到。 I neither want each line integrated into a new list / nested list 我既不希望每行都集成到新列表/嵌套列表中
[['foo', 'bar', ...]]
nor I want them all in a single list. 我也不希望它们全部放在一个列表中。
Thanks for any help and just ask if you need more information! 感谢您的帮助,请问您是否需要更多信息!
You should use a proper serializer like JSON to write your lists. 您应该使用适当的序列化程序(例如JSON)来编写列表。 Then, you can use the same to deserialize them: 然后,您可以使用相同的方法反序列化它们:
import json
# when writing the lists
f.write(json.dumps(temp_list) + "\n")
# when reading
lst = json.loads(line)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.