Posts: 43
Threads: 14
Joined: Jul 2018
Greetings,
can someone tell me wether the following is correct?
I have float values stored in two lists, namely x2 and y2.
I want to print a csv file with delimiter " ", in which the i-th element of x2 stands next to the i-th element of y2 in the i-th row.
with open(outputname, mode='w') as csv_output:
result_writer=csv.writer(csv_output, delimiter=" ")
for j in range(len(x2)):
result_writer.writerow(x2[j], y2[j]) outputname is a string of the name of the outputfile that is to be created, namely "outputfile.csv".
Regards!
Posts: 48
Threads: 0
Joined: Nov 2017
Dec-19-2018, 11:13 AM
(This post was last modified: Dec-19-2018, 11:18 AM by ODIS.)
I would say that you have to pass the list of columns to the writerow() function:
import csv
x2 = [1, 2, 3]
y2 = ["a", "b", "c"]
with open("./outputfile.csv", "w") as file:
csv_writer = csv.writer(file, delimiter=" ")
for i in range(0, len(x2)):
csv_writer.writerow(
[
str(x2[i]),
str(y2[i])
]
)
Posts: 8,160
Threads: 160
Joined: Sep 2016
Dec-19-2018, 11:33 AM
(This post was last modified: Dec-19-2018, 01:03 PM by buran.)
with open(outputname, mode='w') as csv_output:
result_writer=csv.writer(csv_output, delimiter=" ")
# option 1
for nums in zip(x2, y2):
result_writer.writerow(nums) # here you need to pass iterable, e.g. list, tuple, etc.
# option 2 - alternative to option 1
data = (nums for nums in zip(x2, y2))
result_writer.writerows(data) # here you need to pass list of lists, etc. Here we pass generator expression that will yield tuples EDIT: Option 2 can be simplified further
Posts: 43
Threads: 14
Joined: Jul 2018
Dec-19-2018, 12:31 PM
(This post was last modified: Dec-19-2018, 12:32 PM by SchroedingersLion.)
(Dec-19-2018, 11:33 AM)buran Wrote: with open(outputname, mode='w') as csv_output:
result_writer=csv.writer(csv_output, delimiter=" ")
# option 1
for nums in zip(x2, y2):
result_writer.writerow(nums) # here you need to pass iterable, e.g. list, tuple, etc.
# option 2 - alternative to option 1
data = (nums for nums in zip(x2, y2))
result_writer.writerows(data) # here you need to pass list of lists, etc. Here we pass generator expression that will yield tuples
Why so complicated? What would be the problem with ODIS' solution?
Posts: 8,160
Threads: 160
Joined: Sep 2016
Dec-19-2018, 12:47 PM
(This post was last modified: Dec-19-2018, 12:52 PM by buran.)
(Dec-19-2018, 12:31 PM)SchroedingersLion Wrote: Why so complicated? What would be the problem with ODIS' solution? It's simpler than ODIS solution - 2 lines (use either option1 or option2).
What I don't like with ODIS' solution (no offence, please):
1. using range(len(iterable)) to loop - read https://python-forum.io/Thread-Basic-Nev...n-sequence
2. why construct list in each iteration, when you can use zip to combine both lists and iterate simultaneously over both lists?
3. no need to cast variables to str
4. if we write ODIS' solution on two lines
for i in range(0, len(x2)):
csv_writer.writerow([str(x2[i]), str(y2[i])]) or even if we look at it as is, which one is more readable? Compare with
for nums in zip(x2, y2):
result_writer.writerow(nums) or
data = (nums for nums in zip(x2, y2))
result_writer.writerows(data) Of course, it works - feel free to use any one you like most.
Posts: 43
Threads: 14
Joined: Jul 2018
Thank you, I will give it a try this afternoon!
Posts: 8,160
Threads: 160
Joined: Sep 2016
Dec-19-2018, 12:58 PM
(This post was last modified: Dec-19-2018, 01:17 PM by buran.)
Actually option2 can be simplified even further
data = zip(x2, y2)
result_writer.writerows(data) or even as one-liner
result_writer.writerows(zip(x2, y2))
Posts: 1,950
Threads: 8
Joined: Jun 2018
Dec-20-2018, 10:48 AM
(This post was last modified: Dec-20-2018, 10:48 AM by perfringo.)
For SchroedingersLion sake I add one additional tidbit to consider when comparing those two solutions:
(Dec-19-2018, 12:58 PM)buran Wrote: Actually option2 can be simplified even further
data = zip(x2, y2)
result_writer.writerows(data) or even as one-liner
result_writer.writerows(zip(x2, y2))
zip() produces iterator and you can consume it only once. Depending on your needs and in order to avoid nasty surprises you should be aware of following (expected) behaviour:
>>> x = [1, 2, 3]
>>> y = [4, 5, 6]
>>> zipped = zip(x, y) # iterator created
>>> list(zipped) # iterator consumed
[(1, 4), (2, 5), (3, 6)]
>>> list(zipped) # exhausted iterator
[]
>>> list(zip(x, y)) # iterator created and consumed
[(1, 4), (2, 5), (3, 6)]
>>> list(zip(x, y)) # iterator created again and consumed
[(1, 4), (2, 5), (3, 6)]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
|