我正在尝试将另一行值附加到另一列中。这是我第一次使用python,我已经参考了几篇文章,但是我没有设法使其工作。
下面是我的代码段,您可以看到savetoCSV()
写入文件,然后saveQIDtoCSV()
附加到文件。
我正在尝试实现以下输出,即2个单独列中的值。
Name ID
我的代码:
def savetoCSV(detectionItems,filename):
# specifying the fields for csv file
fields = ['HOST_LIST', 'DATETIME', 'QID', 'IP', 'TRACKING_METHOD', 'media']
lstdetectionItems=[detectionItems]
#lstQID = [qid]
# writing to csv file
with open(filename, 'w' , newline= '') as csvfile:
# creating a csv dict writer object
writer = csv.writer(csvfile,delimiter='\n')
for qid in lstdetectionItems:
writer.writerows(lstdetectionItems)
def saveQIDtoCSV(qid, filename):
# specifying the fields for csv file
fields = ['HOST_LIST', 'DATETIME', 'QID', 'IP', 'TRACKING_METHOD', 'media']
lstdetectionItems=[qid]
# writing to csv file
with open(filename, 'a' , newline= '') as csvfile:
# creating a csv dict writer object
writer = csv.writer(csvfile,delimiter=',')
for qid in lstdetectionItems:
f1= open(filename,'r')
reader=csv.reader(f1)
for row in reader:
row[1] = f1.readline()
writer.writerow(row)