我有一个文件,我正在尝试使用Python中的数据填充第二个文件。在填充时我正在应用一些if
语句来操作第二个文件中的一列。
我想使用这些if
语句执行的操作:检查文件中的第31列,如果其值为0
,则在第32列中返回0
值,如果其值为空,则返回第32列中的2
值以及第32列中的每个其他非零和非空值返回1
值。
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)
这似乎是一个相当简单的问题,但我的输出文件给了我错误的结果。对于第31列中的1
值,它在第32列中具有0
值。它在两列中应该是0
。虽然2
值与第31列中的blank
值相对应。
请帮助我解决这个问题,因为在我多次尝试查找我使用的if
语句的逻辑错误之后,我现在一无所知。
实际O / P的片段:(两行中的最后两个条目是相关的列)
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片段:(前2行)(最后2列是相关列)
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,
答案 0 :(得分:3)
答案 1 :(得分:1)
将您的任务分成读取,操作,然后写入数据的函数。
将您的表读入列表:
def importCSV(fname):
""" Returns a tuple using a CSV as input. First tuple is the
header.
Second tuple is a dictionary with all the data.
"""
data = []
with open(fname, 'rb') as csvfile:
reader = csv.DictReader(csvfile, delimiter=',')
header = reader.fieldnames
for lines in reader:
data.append(lines)
return (header, data)
header, data = importCSV('myfile.csv')
现在操纵你的数据:
for line in data:
do something to line...
....
然后写下你的数据:
def CreateCSVFromList(header, source, outputfile):
"""Creates a CSV from a 2D List.
Header should be a simple list for each column in the resulting list.
Source is the list.
Output file is what is written. Include the .csv extension please.
No error-checking is being done at the moment.
"""
import csv
with open(outputfile, 'wb') as csvfile:
writer = csv.writer(csvfile, dialect='excel')
writer.writerow(header)
for row in source:
writer.writerow(row)
CreateCSVFromList(header, data, 'output.csv')
答案 2 :(得分:1)
如果我将逻辑更改为此(请记住,除非您使用int()
,float()
等转换它们,否则所有内容都是字符串。)
for row in reader:
print "'%s'" % row[30]
if float(row[30]) != 0:
row[31] = '1'
else:
row[31] = '0'
if row[30] == "":
row[31] = 2
writer.writerow(row)
我得到了这个输出:
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,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,0
其中最后一个元素为0;那是问题,对吧?
答案 3 :(得分:0)
试试这个:
_buffer = ''
for row in reader:
if reader.line_num == 31:
if row == ['']: #check whether 31st row is empty
_buffer = 2 #if that's true write `2`to line 32
elif row == ['0']: #if that's false check wheter 31st row = 0
_buffer = 1 #if true, write `1`to line 32
else:
_buffer = 1
if reader.line_num == 32:
row = _buffer
writer.writerow(row)