Hi,
I am very new to Python and I would like to know if it is possible to nest 'for' statements?
The question precipitates from a certain problem I am having with running a simple calculation on a column in a .csv file. I want to read columns from an existing .csv file and write out selected columns from the .csv file along with the new column containing the results from the calculation to a new .csv file.
I am thinking the way to do this is to nest for statements so that I am first reading the desired columns, performing an if else statement on the column, and then writing out the results using another for statement.
Maybe I am making this out to be more complicated than it has to be. Like I said, I am new...I am probably missing a crucial step. Here is what I have so far:
1) I am getting an invalid syntax error at 'if LOAD>20'
2) How do I pass this calculation to the results so that it prints a 'greater than 20/less than 20' statement for each row in the column LOAD based upon its value?
Thanks for the help!
I am very new to Python and I would like to know if it is possible to nest 'for' statements?
The question precipitates from a certain problem I am having with running a simple calculation on a column in a .csv file. I want to read columns from an existing .csv file and write out selected columns from the .csv file along with the new column containing the results from the calculation to a new .csv file.
I am thinking the way to do this is to nest for statements so that I am first reading the desired columns, performing an if else statement on the column, and then writing out the results using another for statement.
Maybe I am making this out to be more complicated than it has to be. Like I said, I am new...I am probably missing a crucial step. Here is what I have so far:
Code:
#---Feed TranPy the csv and sys modules--- import csv, sys #---Open source file, read source file, write to new source file--- source = csv.reader(open("H:/transpor/Transit Ridership Database/TranPy/LoadData.csv", "rb")) for TIME, DIR, LOCATION, ON, OFF, LOAD, RUNTIME, LATITUDE, LONGITUDE, SAMPLES in source: print TIME, DIR, LOCATION, LOAD for LOAD in source: if LOAD>20 print 'greater than 20' else: print 'less than 20' results = csv.writer(open("H:/transpor/Transit Ridership Database/TranPy/LoadData_out.csv", "wb")) results.writerows(row)
2) How do I pass this calculation to the results so that it prints a 'greater than 20/less than 20' statement for each row in the column LOAD based upon its value?
Thanks for the help!
Comment