I'm stuck. I currently have the following example file structure:

X, Y, Z
0.000234E+04, 0.000244E+03, 0.000234E+04
0.000244E+03, 0.000234E+04, 0.000238E+05
0.000238E+05, 0.000244E+03, 0.000234E+04

I would like for the Scientific Notation to be converted to float or integer values:

X, Y, Z
2.34, 0.244, 2.34
0.244, 2.34, 23.8
23.8, 0.244, 2.34

I have tried the following code:

p=open('file_with_data.txt')
r=p.readline()
r=p.readline() ##to skip over first row##
y=r.split()
for i in y: float(i)

The code worked converting the sci-notated numbers but it did not return a list. Instead it returned each number individually:
2.34
0.244
2.34

I just ran it on one line. I understand I will need to add a loop mechanism but I wanted to get the first line outputed to a list first as the real dataset has nearly a 160,000 lines...

Any help would be much appreciated!!

Recommended Answers

All 4 Replies

Something like this does the job, I think.

data = '''X, Y, Z
0.000234E+04, 0.000244E+03, 0.000234E+04
0.000244E+03, 0.000234E+04, 0.000238E+05
0.000238E+05, 0.000244E+03, 0.000234E+04'''

numbers = data.splitlines()
print numbers.pop(0)

print '\n'.join(', '.join(str(float(value))
                        for value in numberline.split(',')
                         )
              for numberline in numbers)

Thanks you for your help! that looks like what I'm going for!
My dataset is contianed within a .txt file. When I replace 'data' with my open file the program fails :(

p=open(r'C:\file.txt', 'r')
numbers = p.splitlines()
print numbers.pop(0)
print '\n'.join(', '.join(str(float(value))
for value in numberline.split(' , ')
)
for numberline in numbers)

and I receive the following error:

Traceback (most recent call last):
File "C:/Documents and Settings/xxxxavd/Desktop/testtrial2.py", line 3, in <module>
numbers = p.splitlines()
AttributeError: 'file' object has no attribute 'splitlines'

Do I read each line of my .txt file in separately via p.readline(), then p.splitline() and run the process from there looping line by line?

p=open(r'C:\file.txt', 'r')
numbers = p.splitlines()
print numbers.pop(0)
print '\n'.join(', '.join(str(float(value))
                        for value in numberline.split(' , ')
                         )
              for numberline in numbers)

I thought to leave something for you to do also, but OK, try this:

with open('file.txt') as p:
    print p.next(),
    print '\n'.join(
        ', '.join(str(float(value))
                  for numberline in numbers.rstrip().split(' \n') # take out '\n' by rstrip()
                  for value in numberline.split(','))
        for numbers in p)

thanks so much!

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.