I have one file and I am trying to populate second file using its data in Python. While populating I am applying some if
statements to manipulate one column in the 2nd file.
What I want to perform using these if
statements: Check 31st column in file and if its value is 0
then return 0
value in 32nd column, if its value is blank then return 2
value in 32nd column and for every other non-zero and non-blank value return 1
value in 32nd column.
with open('DE_Combined_' + time.strftime("%Y-%m-%d")+".csv", "rb") as in_file, open('DE_Combined_PD_' + time.strftime("%Y-%m-%d")+".csv", "wb") as out_file:
reader = csv.reader(in_file)
writer = csv.writer(out_file)
headers = next(reader, None) # returns the headers or `None` if the input is empty
if headers:
writer.writerow(headers)
for row in reader:
if row[30] != 0:
row[31] = 1
else:
row[31] = 0
if row[30] == "":
row[31] = 2
writer.writerow(row)
This seems like a pretty straightforward problem but my output file is giving me the wrong results. It has 1
value in 32nd column for 0
value in 31st column. It should have been 0
in both columns. Although 2
value is coming against the blank
value in 31st column.
Please help me in resolving this problem as I am clueless now after several attempts to find something wrong with the logic of if
statements I have used.
Snippet of actual O/P: ( last 2 entries in both the rows are the columns concerned)
05/23/2013 May 2013 2013 4 2 Thursday UK O2 £ NOKIA 100 NOKIA 100 Smartphone Symbian NA 9.99 N Pay & Go Pay & Go 0.64222 0 1
05/23/2013 May 2013 2013 4 2 Thursday UK O2 £ NOKIA 100 NOKIA 100 Smartphone Symbian NA Pink 9.99 N Pay & Go Pay & Go 0.64222 0 1
I/P Snippet:(First 2 rows) ( Last 2 columns are the concerned columns)
Date,Month,Fiscal_Year,Calendar_Year,FY_Quarter,CY_Quarter,Day_of_Week,Geography,MO,Currency,Device_OEM,Device_Name,GDN,Device_Type,Device_OS,Device_Franchise,Device_Color,Device_Storage_in_GB,Device_Price,Device_Monthly_Price,Additional,Device_Refurb,Plan_COMPARISON,Plan_Name,Plan_Contract_Length,Plan_Monthly_Price,Plan_Data_in_GB,Plan_Minutes,Plan_Texts,Exchange_Rate_vs_1USD,Difference,Difference_Flag
05/23/2013,May,2013,2013,4,2,Thursday,UK,O2,£,NOKIA,100,NOKIA 100,Smartphone,Symbian,NA,,,9.99,,,N,,Pay & Go,Pay & Go,,,,,0.64222,0.0,
05/23/2013,May,2013,2013,4,2,Thursday,UK,O2,£,NOKIA,100,NOKIA 100,Smartphone,Symbian,NA,Pink,,9.99,,,N,,Pay & Go,Pay & Go,,,,,0.64222,0.0,