Pregunta

This script reads a text file, takes averages of every 3 rows for each column and writes it into a csv file:

Input file:

2013-08-29T15:11:18.55912   0.019494552 0.110042184 0.164076427 0.587849877
2013-08-29T15:11:18.65912   0.036270974 0.097213155 0.122628797 0.556928624
2013-08-29T15:11:18.75912   0.055350041 0.104121094 0.121641949 0.593113069
2013-08-29T15:11:18.85912   0.057159263 0.107410588 0.198122695 0.591797271
2013-08-29T15:11:18.95912   0.05288292  0.102476346 0.172958062 0.591139372
2013-08-29T15:11:19.05912   0.043507861 0.104121094 0.162102731 0.598376261
2013-08-29T15:11:19.15912   0.068343545 0.102805296 0.168517245 0.587849877
2013-08-29T15:11:19.25912   0.054527668 0.105765841 0.184306818 0.587191978
2013-08-29T15:11:19.35912   0.055678991 0.107739538 0.169997517 0.539165352

Script:

data = loadtxt('infile.txt', usecols = (1,2,3,4))
with open ('out.csv', 'wb') as outfile:
    writer = csv.writer(outfile, delimiter = '\t')
    seg_len = 3       
    for x in range(0, len(data[:,1]), seg_len):
        sample_means = ([x/seg_len], [mean(data[x:x+seg_len,i]) for i in range(4)])
        bi = list(chain.from_iterable(sample_means))
        writer.writerow ((', '.join(map(repr, bi))))

Output CSVfile:

0    0.037038...    0.10379...  ...
1    0.051183...    0.10466...  ...
2    00059516...    0.10543...  ...

But when I open the csv file and sum columns it gives zero! Seems like they're not numbers. I copied directly from CSV file and it looks like this:

"0      "   "   0   .   0   3   7   0   3   8   5   2   2   3   3   3   3   3   3   3   3       "   "   0   .   1   0   3   7   9   2   1   4   4   3   3   3   3   3   3   3   2       "
"1      "   "   0   .   0   5   1   1   8   3   3   4   7   9   9   9   9   9   9   9   9   7       "   "   0   .   1   0   4   6   6   9   3   4   2   6   6   6   6   6   6   6   6       "
"2      "   "   0   .   0   5   9   5   1   6   7   3   4   6   6   6   6   6   6   6   6   1       "   "   0   .   1   0   5   4   3   6   8   9   1   6   6   6   6   6   6   6   6       "

Can someone advise how to resolve this please?

¿Fue útil?

Solución

Perhaps you are looking for this?

sample_means = [x/seg_len] + [mean(data[x:x+seg_len,i]) for i in range(4)]
writer.writerow(sample_means)

Otros consejos

Part of your problem is that writerow expects an iterable of values. You're giving it a string (the output of ', '.join), so each letter in the string gets written to the file in a separated delimited field. If you pass bi directly to writerow it should at least solve that particular problem.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top