0

Have for example three lists:

list4 = (start_time, 'Test type1', 'Result1', 'Units1')
list5 = (start_time, 'Test type2', 'Result2', 'Units2')
list6 = (start_time, 'Test type3', 'Result3', 'Units3')

And then - they used in:

report = open('111.csv', 'w')
writer = csv.writer(report, delimiter=';')

#writer = csv.writer(sys.stdout, delimiter=';')

for row in zip(list4, list5, list6):
   writer.writerow(row)

And this leads to this result:

$ cat 111.csv
2014-10-01 16:53:29;2014-10-01 16:53:29;2014-10-01 16:53:29
Test type1;Test type2;Test type3
Result1;Result2;Result3
Units1;Units2;Units3

But _ want - it create file like:

2014-10-01 16:51:21;Test type1;Result1;Units1;
2014-10-01 16:51:21;Test type2;Result2;Units2;
2014-10-01 16:51:21;Test type3;Result3;Units3;
2
  • Seems like you want to transpose your data. So instead of zip(list4, list5, list6) just (list4, list5, list6) without zip? Commented Oct 1, 2014 at 10:00
  • OMG! :-) Could you add you comment as answer? Commented Oct 1, 2014 at 10:06

2 Answers 2

1

Seems like you want to transpose your data. So instead of zip(list4, list5, list6) just use (list4, list5, list6) or [list4, list5, list6]:

>>> zip(list4,list5,list6)
[(1, 1, 1), ('Test type1', 'Test type2', 'Test type3'), ('Result1', 'Result2', 'Result3'), ('Units1', 'Units2', 'Units3')]

but

>>> [list4,list5,list6]
[(1, 'Test type1', 'Result1', 'Units1'), (1, 'Test type2', 'Result2', 'Units2'), (1, 'Test type3', 'Result3', 'Units3')]
Sign up to request clarification or add additional context in comments.

Comments

1

you seem to be confused between tuples and list in python. any way second pythonic approach would be to append available tuples into a blank list. This way you could dynamically manipulate available data for csv writing instead of hard coding it:

csv_data = []
a = (start_time, 'Test type1', 'Result1', 'Units1')
b = (start_time, 'Test type2', 'Result2', 'Units2')
c = (start_time, 'Test type3', 'Result3', 'Units3')

if len(a)==4 :#eg condition: check if a has 4 elements
    csv_data.append(a)

#some other condition
csv_data.append(b)

#check if start_time is 1 day ago
csv_data.append(c)
#manipulated csv data is available in list csv_data
print csv_data

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.