如何将列表列表写入文件 [英] How write a list of list to file

查看:89
本文介绍了如何将列表列表写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的列表

I have a list that looks like this

list_geo = [[5], ['Optimized energy: -39.726863484331 E_h'],
    ['C\t', '-1.795202\t', ' 0.193849\t', ' 0.019437'],
    ['H\t', '-0.728046\t', '0.337237\t', ' 0.135687'],
    ['H\t', '-2.044433\t', '-0.840614\t', ' 0.220592'],
    ['H\t', '-2.085087\t', ' 0.444715\t', '-0.993886'],
    ['H\t', '-2.323267\t', ' 0.834105\t', ' 0.714902']]

我想将此写入文件outputfile,并且尝试使用

I want to write this to file outputfile, and I tried to use

with open(outputfile, "w") as output_file:
    output_file.write(list_geo)

但这不起作用.加入list_geo也不起作用.我该如何保存?

But this does not work. Joining list_geo also does not work. How can I save this?

推荐答案

您需要使用for循环

with open("path /of/op/file.txt", "w") as output_file:
   for value in list_geo:
       output_file.write(str(value))

这篇关于如何将列表列表写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆