我有一个用例,可以读取csv文件(inputs.csv)并为csv的每一行调用API端点。并将输出写入不同的csv(outputs.csv),并按特定列对输出csv进行排序。我可以通过以下代码实现所有这些目标。需要找出我是否可以通过多线程方式更有效地进行操作。
def main():
start = time.time()
print "read from csv file "
input_file = 'inputs.csv'
output_file = 'outputs.csv'
read_write_csv(input_file, output_file)
print("after output-->",time.time() - start)
sort_csv(output_file)
print("after sort -->",time.time() - start)
def read_write_csv(input_file, output_file):
with open(input_file, 'r') as csv_file:
reader = csv.reader(csv_file)
# Reading row by row
count = 0
for row in reader:
# Opening csv result file in append mode
with open(output_file, "a+") as csv_save:
writer = csv.writer(csv_save)
print "mac address ", row[0]
writer.writerow([row[0], callExternalAPI(row[0])]),(row,))
print "{0} devices processed so far".format(count+1)
csv_save.close()
def sort_csv(output_file):
with open('sorted.csv', 'w') as csv_final:
r = csv.reader(open(output_file), delimiter=",")
writer_final = csv.writer(csv_final)
sortedResponse = sorted(r, key=operator.itemgetter(1), reverse=True)
for row in sortedResponse:
writer_final.writerow(row)
main()
如您所见,我是一名python新手,因此欢迎您提出任何改进代码的建议。