I would like to write a 2-D array to a Python 3 txt file.
e.g.
My_list = ['Hello', 'World', 0], ['Pretty', 'World', 1], ['Tired', 'World', 2]]
going into my_text.txt.
I have tried loads of approaches none of which I can recommend as I get a variety of results including 1 element Lists:
["['Hello', 'World', 0], ['Pretty', 'World', 1], ['Tired', 'World', 2]"]
and lists of strings:
["[Hello', 'World', 0]", "['Pretty', 'World', 1]", "['Tired', 'World', 2]"]
along with other wonderful results. Does anyone know some of some simple straightforward code for this or a tutorial? Just doing this out of curiosity, tbh and am struggling badly.
I would like to be able to read my List out from the file again and use it fully as a List once again
e.g, print(my_list[0][0])
yields 'Hello'
答案 0 :(得分:2)
json
is good at serializing lists/dicts/numbers/strings:
import json
My_list = [['Hello', 'World', 0], ['Pretty', 'World', 1], ['Tired', 'World', 2]]
#write to file
with open("data.json", "w") as file:
json.dump(My_list, file)
#read from file
with open("data.json") as file:
new_list = json.load(file)
print(new_list)
Result:
[['Hello', 'World', 0], ['Pretty', 'World', 1], ['Tired', 'World', 2]]
答案 1 :(得分:0)
还要考虑 yaml 。需要安装 pyyaml (push()
)。
pip install pyyaml
将列表对象保存到文件:
import yaml
输出文件如下:
my_list = [['Hello', 'World', 0], ['Pretty', 'World', 1], ['Tired', 'World', 2]]
with open('my_list.yml', 'w') as outfile:
yaml.dump(my_list, outfile, default_flow_style=False)
要重新加载列表:
- - Hello
- World
- 0
- - Pretty
- World
- 1
- - Tired
- World
- 2
ast.literal_eval
,这是一个简单的示例,您可以进一步自定义。
with open("my_list.yml", 'r') as inputfile:
my_list_back = yaml.load(inputfile)
比基本的读/写操作可能是:
import ast
string_list = (str(my_list)) # convert tostring then save it to file
print(string_list.__class__) # it's a string
reconverted_list = ast.literal_eval(string_list) # convert back with ast
print(reconverted_list.__class__) # it's a list
答案 2 :(得分:0)
嗨,有兴趣的人。
我想将数组保存到Python文本文件中并完全检索它,以便可以处理所有元素。
我确定要解决我的问题,并用非常混乱的代码解决了这个问题。
下面的代码完成了我想做的事情。
没有意义的运动,但我只需要做。
感谢您的帮助和想法。
my_list = []
my_list_669 = []
def create_list():
#Creating the list
for x in range(5):
my_list.append(["Hello", "World", x])
print("my_list = ", my_list)
def save_list_to_file():
#creating the string
string_1 = ""
for item in my_list:
string = item[0] + "," + item[1] + "," + str(item[2]) + "\n"
string_1 += string
#adds records to a string with a line return after each record
with open('your_file.txt', 'w') as f:
f.write(string_1)
def recover_list():
with open('your_file.txt', 'r') as f:
tiing = f.read().splitlines()
#splits lines at \n and inserts into array called 'tiing'
#each item is equivalent to a record
for items1 in tiing:
my_list_69 = items1.split(",")
#splits the array items in ting at "," mark
#these are now in an array called 'my_list_69'
#below I access all items from within the list
#and append them to a temporary sub-list
sub_list = []
for items in my_list_69:
sub_list.append(items)
my_list_669.append(sub_list) this reconstructs the list
create_list()
save_list_to_file()
recover_list()
Testing:
print(my_list_669)
print(my_list_669[0])
print(my_list_669[0][2])
for items in my_list_669:
print(items)