0

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?

2
  • 1
    To clarify, is the section under "Output CSV" what you expect and the one below that what you got?
    – thegrinner
    Nov 25, 2013 at 21:50
  • Output file shows exactly what I expect. but as I said they just look like numbers, but you cannot sum, average... they should be numbers so that I can perform statistical analysis. the one under is copied from csv cells and pasted here. I think this is the reason why they're not counted as numbers but don't know how to approach.
    – PyLearner
    Nov 25, 2013 at 21:56

2 Answers 2

4

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)
3
  • this writes both lists together in a single cell, while I want them in separate columns.
    – PyLearner
    Nov 25, 2013 at 22:04
  • No, it really doesn't. Are you sure '\t' is the correct delimiter for whatever you're using to open the resulting file? Nov 25, 2013 at 22:05
  • You're right, removing '\t' solved the issue. Thank you soooo much :)
    – PyLearner
    Nov 25, 2013 at 22:08
0

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.

2
  • it does, but writes two separate lists. I added bi to remove brackets. probably was not the best way of ding it!
    – PyLearner
    Nov 25, 2013 at 22:00
  • Using bi to flatten your tuple of two lists should be fine, if overcomplicated. There are simpler ways, like the construction freakish's answer uses. Nov 25, 2013 at 22:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.