954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Converting a simple list

Hello friends,

I have a list similar to ['9.48E+02', '9.42E+02', '9.79E+02', '9.58E+02', '9.28E+02', '9.47E+02'].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

kaskoraja
Newbie Poster
10 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

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

griswolf
Veteran Poster
1,165 posts since Apr 2010
Reputation Points: 344
Solved Threads: 256
 

temperature data extracted.dat has the following list ie ['9.48E+02', '9.42E+02', '9.79E+02', '9.58E+02', '9.28E+02', '9.47E+02']
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 ['9.48E+02', '9.42E+02', '9.79E+02', '9.58E+02', '9.28E+02', '9.47E+02']...can you please check my program.

-Thanks for your quick reply

kaskoraja
Newbie Poster
10 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

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

kaskoraja
Newbie Poster
10 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

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.

griswolf
Veteran Poster
1,165 posts since Apr 2010
Reputation Points: 344
Solved Threads: 256
 

YEs, my actual format of temperature data extracted.dat is
['9.48E+02', '9.42E+02', '9.79E+02', '9.58E+02', '9.28E+02', '9.47E+02']

kaskoraja
Newbie Poster
10 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

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 ifThe 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

griswolf
Veteran Poster
1,165 posts since Apr 2010
Reputation Points: 344
Solved Threads: 256
 

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
kaskoraja
Newbie Poster
10 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: