I used the open() function to save the contents of the list:

 my_list = ['1', '2', 'a'] save = open('items.txt', 'w') save.write(', '.join(my_list)) save.close 

You need to add the saved data to the sheet, but when using the append() function append() shell produces None :

 my_list = [] new = open('items.txt') # 'items.txt' has "1, 2, a" as a string. for item in new: print(my_list.append(item)) 

Why it happens?

  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

2 answers 2

append modifies the list and returns nothing by itself.

So it will be right:

 my_list = ['1', '2', 'a'] with open('items.txt', 'w') as f: f.write(', '.join(my_list)) with open('items.txt', 'r') as f: text = f.read() new_list = text.split(', ') print(new_list) # ['1', '2', 'a'] 

Slightly more

It is better to work with files through with , so that they are automatically closed and you do not have to call the close method yourself.

You need to understand what type of new in your example:

 type(new) # <type 'file'>` 

Accordingly, iteration over new will return file lines, rather than individual characters. However, the code from the question would not work in the opposite case, since spaces and commas are no worse than letters and numbers from the point of view of Python. We would get a list:

 ['1', ',', ' ', '2', ',', ' ', 'a'] 

Well, the list in Python is a variable data structure, append adds an element to the list and does not return anything (more precisely, it returns None, which you successfully print to the screen).

    close is a method to call it you must write parentheses

     save.close() 

    then the file will be closed.