Hi!

I've got an assignment to read an input file and do some calculation and a graph and fancy stuff like that. The problem I am having is reading the file. I get a file that looks like this:

0.00	1.776e-12	3.756e-07	0.000e+00	0.000e+00
0.20	1.769e-12	2.377e-07	0.000e+00	0.000e+00
0.40	1.764e-12	2.145e-07	0.000e+00	0.000e+00
0.60	1.759e-12	2.177e-07	0.000e+00	0.000e+00

with the tab-whitespaces between columns, and I need to get each column into a vector so I can make calculations with them. I've been trying to do stuff like readline() to get the line, but I have no idea how to extract the numbers from the line, that is, I'm stuck at:

line = 0.00\t2.869e-12\t7.715e-08\t0.000e+00\t0.000e+00\n

Anyone got some tips?

Use regular expressions.

Or use the split method.

I'd use regexp's because you've got both \t AND \n. Though I suppose that's a lot of extra overhead (someone tell me? I'm not sure how efficient regexps are compared to say string manipulation methods), so it's probably better to just go:

lineList = line.split('\t')
lineList = lineList[:-1] + [lineList[-1].split('\n')[0]]

Or in more explicit terms:

Split the list at every tab. Now take the last element of the new-formed list, and split it at the newline '\n'. This will give you a list with two elements: a number and a newline. Take the first element of the sublist and concatenate it to the original list of numbers up to it's last element. Id est:

line = '0.00\t2.869e-12\t7.715e-08\t0.000e+00\t0.000e+00\n'
lineList = line.split('\t') 
# this gives you ['0.00', '2.869e-12', '7.715e-08', '0.000e+00', '0.000e+00\n']
temp = lineList[0]
# this gives you '0.000e+00\n'
tempList = temp.split('\n')
# this gives you ['0.000e+00', '']
tempList = [tempList[0]]
# this gives you ['0.000e+00']
lineList = lineList[:-1]
# this gives you ['0.00', '2.869e-12', '7.715e-08', '0.000e+00']
lineList += tempList
# this gives you the final result of ['0.00', '2.869e-12', '7.715e-08', '0.000e+00', '0.000e+00']

Wow, thank you very much, saved me quite a bit of work there :)

Np. It would be wise to make sure you understand what I wrote and not just copy that code straight into your programme, though.

Of course, else I wouldn't be learning more. My program also does a lot more than just edit that one file, so I'll have to modify that anyway :)

Fuse, very nice code ad explanation!
Python has a few string functions that make your code even easier. Simply use line.rstrip(). The default removes any trailing whitespace characters like '\n':

line = '0.00\t2.869e-12\t7.715e-08\t0.000e+00\t0.000e+00\n'
# remove any trailing whitspace characters (default)
line = line.rstrip()
line_list = line.split('\t')
print line_list  # ['0.00', '2.869e-12', '7.715e-08', '0.000e+00', '0.000e+00']
commented: thanks, didn't know that! +1
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.