Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将列表写入文件_Python_List_File Io_File Writing - Fatal编程技术网

Python 如何将列表写入文件

Python 如何将列表写入文件,python,list,file-io,file-writing,Python,List,File Io,File Writing,我有一张这样的清单 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',

我有一张这样的清单

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
,并尝试使用

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

但这是行不通的。加入
列表\u geo
也不起作用。如何保存此文件?

您需要使用for循环

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

您不能将列表直接写入文件,但可以写入字符串/字符流

将列表转换为字符串,然后将字符串写入文件

data = ['1','2'] 
data_string = ','.join(data) # create string and then store

with open('f.txt','w') as f:
        f.write(data)

我想你应该把列表中的每一个子列表都写成一行

那么这就是你需要的:

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']]

with open("file.txt", "w") as output_file:
   for line in list_geo:
       output_file.write("".join([str(i) for i in line]))
       output_file.write("\n")
运行脚本后
file.txt
的内容:

5
Optimized energy: -39.726863484331 E_h
C       -1.795202       0.193849       0.019437
H       -0.728046       0.337237       0.135687
H       -2.044433       -0.840614      0.220592
H       -2.085087       0.444715       -0.993886
H       -2.323267       0.834105       0.714902
说明:

  • “”.join()
    获取字符串列表,并将它们连接在一起,中间没有空格
  • [str(i)for i in line]
    将列表中的每个元素转换为字符串(仅当列表中的第一个子列表为
    [5]
    时才需要)

有没有办法把多余的空间保留为正数

如果你想给你的行添加格式,你最好先把这些值作为实数,也就是说,不要用
['C\t','1.795202\t','0.193849\t','0.019437']
你需要
['C',-1.795202,0.193849,0.019437]
,然后用
“{}\t{}\t{:>9f}\t}\t}>格式(*>9f}\n./code>

如果您只是从一个无法更改的脏源获取此列表,那么您需要以某种方式对该行进行预处理,例如:

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']]

with open("file.txt", "w") as output_file:
   for line in list_geo:
        if len(line) == 4:
            # treat first element as string, convert rest to float
            line = [el.strip() if i == 0 else float(el) for i,el in enumerate(line)]
            line_formatted = "{}\t{:>9f}\t{:>9f}\t{:>9f}\n".format(*line)
            output_file.write(line_formatted)
        else:
            output_file.write("".join([str(i).strip(" ") for i in line]))
            output_file.write("\n")
其结果是:

5
Optimized energy: -39.726863484331 E_h
C       -1.795202        0.193849        0.019437
H       -0.728046        0.337237        0.135687
H       -2.044433       -0.840614        0.220592
H       -2.085087        0.444715       -0.993886
H       -2.323267        0.834105        0.714902

最后的文本应该是什么样子?永远不要说“这不起作用”。说为什么。包括错误/异常。在您的情况下,由于无法传递
file.write()
列表,因此它会给出
TypeError:应为字符串或其他字符缓冲区对象。因此,很明显,您需要找出如何从列表中生成字符串。已经有了,这是重复的。重复的,还有…人,关于同一主题的22个问题是一团糟。是时候规范化了。这是你没有问但应该问的问题的答案。看起来您的数据来自一个以制表符分隔的文件,您只需按原样读取它,甚至不需要去除
\t
分隔符,也不需要删除存储的数字字符串表示形式(
'-0.840614\t'
)。而不是存储实际的数字。用它来代替。这会让你的生活更轻松。当您想写出数据时,请阅读有关数字格式的内容。有没有办法将多余的空间保留为正数?在哪里?在第一行?什么额外的空间?啊,我明白了,它们不是均匀排列的。这是因为你的一些数字中有空格,而有些没有。等等,我再加一句this@Yoda:您现在正在询问数字的字符串格式。如果要将它们格式化,确实不应该使用尾随空格存储数字的字符串表示形式(
'-0.840614\t'
)列表,而应该存储数字本身的列表。并在编写它们时应用正确的格式。@Yoda:我现在通过在数组中添加一个空格来修复缩进,因为在python中修复空格变得非常混乱和粗糙