Python 将二维列表写入文件,读取并重新使用保存的列表
我想在Python3TXT文件中编写一个二维数组 e、 g 进入my_text.txt 我尝试了很多方法,但没有一种我可以推荐,因为我得到了各种结果,包括1个元素列表:Python 将二维列表写入文件,读取并重新使用保存的列表,python,list,file,text,Python,List,File,Text,我想在Python3TXT文件中编写一个二维数组 e、 g 进入my_text.txt 我尝试了很多方法,但没有一种我可以推荐,因为我得到了各种结果,包括1个元素列表: ["['Hello', 'World', 0], ['Pretty', 'World', 1], ['Tired', 'World', 2]"] 和字符串列表: ["[Hello', 'World', 0]", "['Pretty', 'World', 1]", "['Tired', 'World', 2]"] 以及其他精彩
["['Hello', 'World', 0], ['Pretty', 'World', 1], ['Tired', 'World', 2]"]
和字符串列表:
["[Hello', 'World', 0]", "['Pretty', 'World', 1]", "['Tired', 'World', 2]"]
以及其他精彩的结果。
有人知道一些简单明了的代码吗?
只是出于好奇,tbh这么做,我挣扎得很厉害
我希望能够再次从文件中读取我的列表,并再次将其完全用作列表
e、 g,
print(my_list[0][0])
产生'Hello'
json
擅长序列化列表/目录/数字/字符串:
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)
结果:
[['Hello', 'World', 0], ['Pretty', 'World', 1], ['Tired', 'World', 2]]
还应考虑yaml。需要安装pyyaml(
pip安装pyyaml
)
将列表对象保存到文件:
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
要加载回列表,请执行以下操作:
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
而基本读/写操作可能是:
with open('my_list.txt', 'w') as file:
file.write(str(my_list))
with open('my_list.txt', 'r') as file:
my_list_back = ast.literal_eval(file.read())
大家好,有兴趣的人 我想将一个数组保存到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)
你能发布一些你试过的代码吗?然后,我们可以为您提供可能走错方向的指针…@LoosaBway“我希望能够再次从文件中读取我的列表,并再次将其完全用作列表”问题是您只是将列表的字符串表示形式写入文件,并调用序列化。正如这里的其他答案所建议的,您只需使用一种内置的序列化格式,JSON、YAML、pickle(尚未提及)。否则,您必须自己解析字符串表示。重要的一点是,保存在文件中的内容不是列表,但由于我花了几个小时在这个练习中,我认为(?)各种python文本文件指令倾向于在保存/检索的内容中添加语音标记,因此产生了困难,我想我想解决这个问题,只是为了它。我真的很想战胜仅仅使用字符串和文件读取方法的挑战-处理各种语音标记似乎是一件非常困难的事情。如果我明白你的意思,我在我的帖子中添加了一个编辑。您需要将e字符串解释为另一个对象(本例中为列表),以便查看。
with open('my_list.txt', 'w') as file:
file.write(str(my_list))
with open('my_list.txt', 'r') as file:
my_list_back = ast.literal_eval(file.read())
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)