简体   繁体   English

如何将文件的每一行读取到单独的列表中以分别处理它们

[英]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)
  1. Use Pickle or JSON to serialize/deserialize your data 使用PickleJSON序列化/反序列化您的数据
  2. If you absolutely need to do your way, you can use ast.literal_eval You can get some help here 如果您绝对需要这样做,可以使用ast.literal_eval您可以在此处获得一些帮助

暂无
暂无

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

相关问题 如何从文本文件中读取 plot 个点,分别读取每一行文本? - How to read plot points from a text file, reading each line of text individually? 如何从 stream 读取 CSV 文件并在写入时处理每一行? - How to read a CSV file from a stream and process each line as it is written? 如何在文件中转换单独的数据并将每一行转换为列表? - How to turn separate data in a file and turn each line into a list? 将每个文件的内容读入Python的单独列表中 - Read the contents of each file into a separate list in Python 将列表的每一行复制到单独的文本文件中 - Copy Each Line of a List into a Separate Text File 有没有办法使用像列表这样的midi文件,其中每个元素都是一个音符-并单独播放它们? - Is there a way to use a midi file like a list where each element is a note - and play them individually? 如何在Python中将文件中的每一行逐字读取到列表中 - How to read each line from a file into list word by word in Python 如何将文件中的每一行存储在单独的数组中? - How to store each line in a file in a separate array? 如何从文件中读取每一行,识别其中包含的变量,然后在主程序中对它们进行排序 - How do I read each line from a file, recognize variables contained within it, and sort them in the main program 如何以行的形式逐行读取文件? - How can I read in my file line by line with each line as a list of floats?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM