0

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. (user_1_list is a fix value, while temp_list is not)

Here's the context of the question: I want every line to be processed in my similarity_score function. 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).

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!

Paprikamann
  • 103
  • 1
  • 8
  • 1) yes, reading a text file gives you text, not lists 2) `pickle` would provide the read/write mechanism you want 3) If you also want readability of the file, use `json` – OneCricketeer Jun 02 '16 at 23:36

2 Answers2

0

You should use a proper serializer like JSON to write your lists. 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)
user2390182
  • 72,016
  • 6
  • 67
  • 89
0
  1. Use Pickle or JSON to serialize/deserialize your data
  2. If you absolutely need to do your way, you can use ast.literal_eval You can get some help here
Community
  • 1
  • 1
user3885927
  • 3,363
  • 2
  • 22
  • 42