How write a list of list to file

て烟熏妆下的殇ゞ 提交于 2019-12-13 20:28:43

问题


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

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)

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


回答1:


You need to use for loop

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



回答2:


You can't write a list directly to file, but you can write a string/stream of characters.

Convert your list into a string and then write the string to the file

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

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



回答3:


I guess want to write every sublist of your list as one line.

Then this is what you need:

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")

Content of file.txt after running the script:

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

Explanation:

  • "".join() takes a list of strings and just concatenates them together without spaces in between
  • [str(i) for i in line] converts every element in the list into string (needed only because you have the [5] as the first sublist in your list)

Is there a way to preserve the extra space in positive number?

If you want to add formatting to your lines you better have the values as real numbers first, i.e. instead of ['C\t', '-1.795202\t', ' 0.193849\t', ' 0.019437'] you'd need ['C', -1.795202, 0.193849, 0.019437] and then format them with e.g. "{}\t{:>9f}\t{:>9f}\t{:>9f}\n".format(*line).

If you somehow just get this list from a dirty source you cannot change, then you'd need to preprocess the line somehow, e.g. 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']]

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")

Which results in:

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


来源:https://stackoverflow.com/questions/47845258/how-write-a-list-of-list-to-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!