Fixing Text Storing/Loading
You should write each list from get_user_list with a new line generally when storing as text. Also note you don't need to call f.close() when using with as it closes the file for you when it is no longer needed.
with open(str(epoch_time) + '_intercom_Users.txt', 'w') as f:
for item in get_users_list:
f.write(f"{item}\n")
This will allow each item to be read in separately and not have to deal with trying to split up your string later.
Then when you go to read you will have a string representation of a list on each line. You need to do a literal eval and remove your new lines characters as well. You can do this in one list comprehension.
import ast
with open('1556640109_intercom_Users.txt', 'r') as f:
x = f.readlines() # read into a list
x = [ast.literal_eval(x[i].rstrip('\n')) for i in range(len(x))]
It would seem like you don't need the \n since we add it then remove it, but it's an easy way to make each item remain a separate item. Without that you would need to add a separator and read in the single line then do a .split(). Using this method will allow you to read the data at rest easily and read it back in easily as well.
Using Pickle Instead
As others have noted, this isn't a great way to do data serialization which is what it appears you are doing. Python comes preloaded with pickle which can serialize and store any Python data type as is and then read it back it.
You could use it like:
import pickle
def get_users():
counter = 0
for i in users_intercom:
get_users_list.append(users_intercom[counter].split())
counter = counter + 1
if counter > 100:
with open(str(epoch_time) + '_intercom_Users.pickle', 'wb') as f:
pickle.dump(get_users_list, f)
break
And then read it again:
with open('1556640109_intercom_Users', 'rb') as f:
x = pickle.load(f)
Answer from MyNameIsCaleb on Stack OverflowFixing Text Storing/Loading
You should write each list from get_user_list with a new line generally when storing as text. Also note you don't need to call f.close() when using with as it closes the file for you when it is no longer needed.
with open(str(epoch_time) + '_intercom_Users.txt', 'w') as f:
for item in get_users_list:
f.write(f"{item}\n")
This will allow each item to be read in separately and not have to deal with trying to split up your string later.
Then when you go to read you will have a string representation of a list on each line. You need to do a literal eval and remove your new lines characters as well. You can do this in one list comprehension.
import ast
with open('1556640109_intercom_Users.txt', 'r') as f:
x = f.readlines() # read into a list
x = [ast.literal_eval(x[i].rstrip('\n')) for i in range(len(x))]
It would seem like you don't need the \n since we add it then remove it, but it's an easy way to make each item remain a separate item. Without that you would need to add a separator and read in the single line then do a .split(). Using this method will allow you to read the data at rest easily and read it back in easily as well.
Using Pickle Instead
As others have noted, this isn't a great way to do data serialization which is what it appears you are doing. Python comes preloaded with pickle which can serialize and store any Python data type as is and then read it back it.
You could use it like:
import pickle
def get_users():
counter = 0
for i in users_intercom:
get_users_list.append(users_intercom[counter].split())
counter = counter + 1
if counter > 100:
with open(str(epoch_time) + '_intercom_Users.pickle', 'wb') as f:
pickle.dump(get_users_list, f)
break
And then read it again:
with open('1556640109_intercom_Users', 'rb') as f:
x = pickle.load(f)
As already noted you need to add newlines (\n). As you tagged your question with Python3, then you should be able to employ so-called f-strings. That is:
for item in get_users_list:
f.write(f"{item}\n")
If you wish to know more about f-strings you might read this tutorial. Others method of string formatting exist too, so feel free to use that which suits your needs best.
EDIT: Now I readed that you need access to particular items of lists. It is possible to get it working using just text writing to and reading from files, however I suggest you to examine already developed modules for data serialization.
Videos
with open('C:/path/numbers.txt') as f:
lines = f.read().splitlines()
this will give you a list of values (strings) you had in your file, with newlines stripped.
In Python 3.5+, you can also do:
lines = pathlib.Path('C:/path/numbers.txt').read_text().splitlines()
also, watch your backslashes in windows path names, as those are also escape chars in strings. You can use forward slashes or double backslashes instead.
Two ways to read file into list in python (note these are not either or) -
- use of
with- supported from python 2.5 and above - use of list comprehensions
1. use of with
This is the pythonic way of opening and reading files.
#Sample 1 - elucidating each step but not memory efficient
lines = []
with open("C:\name\MyDocuments\numbers") as file:
for line in file:
line = line.strip() #or some other preprocessing
lines.append(line) #storing everything in memory!
#Sample 2 - a more pythonic and idiomatic way but still not memory efficient
with open("C:\name\MyDocuments\numbers") as file:
lines = [line.strip() for line in file]
#Sample 3 - a more pythonic way with efficient memory usage. Proper usage of with and file iterators.
with open("C:\name\MyDocuments\numbers") as file:
for line in file:
line = line.strip() #preprocess line
doSomethingWithThisLine(line) #take action on line instead of storing in a list. more memory efficient at the cost of execution speed.
the .strip() is used for each line of the file to remove \n newline character that each line might have. When the with ends, the file will be closed automatically for you. This is true even if an exception is raised inside of it.
2. use of list comprehension
This could be considered inefficient as the file descriptor might not be closed immediately. Could be a potential issue when this is called inside a function opening thousands of files.
data = [line.strip() for line in open("C:/name/MyDocuments/numbers", 'r')]
Note that file closing is implementation dependent. Normally unused variables are garbage collected by python interpreter. In cPython (the regular interpreter version from python.org), it will happen immediately, since its garbage collector works by reference counting. In another interpreter, like Jython or Iron Python, there may be a delay.
Read a specific item in index i:
item = L[index]
Read a sequence of items from index 'start' to index 'stop':
seq = L[start:stop]
Iterate over a list:
for item in L:
print item
If you need both the index and the item, use the enumerate function:
for index, item in enumerate(L):
print index, item
You can also get the reversed list:
L.reverse()
Get list length:
length = len(L)
And there are so much more operations you can perform on lists.
You can just do list[i] , i being the index of the element to be read.