Skip to content Skip to sidebar Skip to footer

How Write A List Of List To File

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

Solution 1:

You need to use for loop

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

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

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

withopen("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:

5Optimized energy:-39.726863484331E_hC-1.7952020.1938490.019437H-0.7280460.3372370.135687H-2.044433-0.8406140.220592H-2.0850870.444715-0.993886H-2.3232670.8341050.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']]

withopen("file.txt", "w") as output_file:
   for line in list_geo:
        iflen(line) == 4:
            # treat first element as string, convert rest to float
            line = [el.strip() if i == 0elsefloat(el) for i,el inenumerate(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:

5Optimized energy:-39.726863484331E_hC-1.7952020.1938490.019437H-0.7280460.3372370.135687H-2.044433-0.8406140.220592H-2.0850870.444715-0.993886H-2.3232670.8341050.714902

Post a Comment for "How Write A List Of List To File"