Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise
import csv
with open("input.csv") as f:
    d1 = [row[0] for row in csv.reader(f)]
for x in d1:
    if 0<=int(x)<30:
        print("0<=x<30".format(x))
    elif 40<=int(x)<120:
        print("40<=x<120")

My code is shown above, and I am trying to save the output in a .csv file. Is there any solution to send data from elif to csv.writer?

share|improve this question
3  
Please do read the documentation. Everything that you need for this question is right there: docs.python.org/2/library/csv.html – Eduard Daduya 18 hours ago
    
As a note/hint: The way you word your problem also makes me worried about your understanding of if/elif. You do not "send" data from the elif statement. It is just a conditional statement and the code inside will be executed if the condition is fulfilled. The code inside the statement is no different than outside, so you do not have to "send" anything. – mwormser 17 hours ago
    
yes I know, but I question is how to write "someiterable" instead of x in d2.writerow(x)!? @Eduard Daduya – MOS 17 hours ago
    
@mwormser, no I mean making the out put! – MOS 17 hours ago
up vote 0 down vote accepted

Just add open('output.csv', 'w').write("40<=%s<120"%(x)) after your print statement in the elif section.

share|improve this answer

You just reopen with csv.writer like so:

d2 = csv.writer(f)
d2.writerow(x)

and read the documentation linked by @Eduard Daduya !!!!!!

share|improve this answer
    
yes I know, but I question is how to write "someiterable" instead of x in d2.writerow(x)!? @user2740652 – MOS 17 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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