我是编程新手。我目前有如下字典,我想将其写入文本文件(以行分隔)以替换文本文件中的原始内容。我更改了一些值并添加了新键,并且想知道如何去做。
下面是我想用其替换原始文本文件的字典:
cars={'Honda\n':['10/11/2020\n','red\n','firm and sturdy\n'
'breaks down occasionally\n'],'Toyota\n':['20/12/2005\n','indigo\n'
'big and spacious\n', 'fuel saving\n'],'Maserati\n':['10/10/2009\n','silver\n','fast and furious\n','expensive to maintain\n'],'Hyundai\n':['20/10/2000\n','gold\n','solid and reliable\n','slow acceleration\n']
原始文件:
Honda
10/11/2010
blue
strong and sturdy
breaks down occasionally
Toyota
20/15/2005
indigo
big and spacious
Maserati
10/10/2009
silver
fast and furious
expensive to maintain
accident prone
所需文件:
Honda
10/11/2020
red
firm and sturdy
breaks down occasionally
Toyota
20/12/2005
indigo
big and spacious
fuel-saving
Maserati
10/10/2009
silver
fast and furious
expensive to maintain
Hyundai
20/10/2000
gold
solid and reliable
slow acceleration
这是我所做的:
with open('cars.txt', 'w') as f:
f.write(str(cars))
f.close()
但它只打印字典而不是所需的文件。我可以知道该怎么做吗?
Qyouu
喵喔喔
慕尼黑8549860
幕布斯6054654
蓝山帝景
相关分类