Hello friends,

I have a list similar to .List has around 9000 elements. I need to convert this list in to file with a format similar to
9.48E+02
9.42E+02
9.79E+02
9.58E+02
9.28E+02
9.47E+02

so that i can access it in another mechanical software

i.e without any commas and ''.This is my first question in the forum.So sorry for my way of questioning if it seems inappropriate to any one

Recommended Answers

All 7 Replies

You can convert the list to a string with one item per line like this:

my_list = [ ... ]
my_list_as_string = '\n'.join(str(x) for x in my_list)

Then you simply write that string to the file. Be careful about adding a newline after the last element. You can use a format specifier instead of str(x) if you need to control the precision.

Alternatively, you can iterate through the list, writing each element to the file:

with open('somefile','w') as f:
  for item in my_list:
    f.write("%.2f\n"%item) # or whatever format works

This one automatically puts the newline after the last item.

Edit: On second look, I see that the values are already strings. That simplifies things. The first example simply uses '\n'.join(my_list) and the second just writes each item (with newline).

temperature data extracted.dat has the following list ie
my_list_as_string=[]
out2=open('final temperature data with lines.dat','w')
my_list=open('temperature data extracted.dat')
my_list_as_string = '\n'.join(str(x) for x in my_list)
print(my_list_as_string,file=out2)

I tried to work this way as you mentioned but still out2 has the same format ie ...can you please check my program.

-Thanks for your quick reply

while using second method i am getting following error
f.write("%.2f\n"%item)
TypeError: a float is required

First: Please learn to use the CODE button so your code has line numbers, indentation, etc. Very helpful.

Second, I am not sure of the actual format of the file 'temperature data extracted.dat' That is the key. You must deal with that exact format, whatever it is. I begin to think it is just one very long line, with commas and single quote marks in that line (as well as the "real" data). Is that true?

Third: You need to understand what you want, what you have, and what steps to take to convert from 'have' to 'want'. Just waving someone else's code at it is unlikely to work, much less work well.

Fourth and last: It is important when thinking, and when writing, to keep categories clear; A list is not a file is not a string. For example, thinking about a file while calling it a list leads to confusion for both yourself and your readers.

YEs, my actual format of temperature data extracted.dat is

A single line. OK then the code reads in the one line from the file, into a variable. Say list_as_string = f.readline() . Then you convert that string into an actual list: actual_list = eval(list_as_string.strip()) and finally, you deal with the list elements however you want. Perhaps

with open('output','w') as ofile
  ofile.write('\n'.join(actual_list)+'\n')

This will only work if

  • The data is in fact all on one line
  • The data is correctly formatted as the string representation of a list

Before you try to use this code, ask yourself if you understand it. If not, read about the with statement and string's join method

I figured it out..

list=['9.48E+02', '9.42E+02', '9.79E+02', '9.58E+02', '9.28E+02', '9.47E+02']
print (','.join(list))
print ('\n'.join(list))

[

OUTPUT IS

9.48E+02
9.42E+02
9.79E+02
9.58E+02
9.28E+02
9.47E+02
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.