首页 > 解决方案 > 如何提高 csv 读写器脚本的性能?

问题描述

我的脚本从输入 csv 读取图像链接,并对每个图像执行质量检查。然后它将检查中的数据附加到新的 csv 中。显然,对每一行进行多次检查将是一个限制因素,但我正在尝试确定一种加快处理速度的方法。我想过一次读多行,但这有帮助吗?有什么建议的方法来加快这个脚本的速度?

# runs each check as needed
def add_to_row(row, line_num):

    # my processing checks would go here

    #appending score and check results
    for idx, i in enumerate(results):
        row.append(i)

# deals with csv file
def add_column_in_csv(input_file, output_file, transform_row):
    # open input file and create output file
    with open(input_file, 'r') as read_obj, \
        open(output_file, 'wb') as write_obj:
        # create a csv.reader object from the input file object
        csv_reader = reader(read_obj)
        # create a csv.writer object from the output file object
        csv_writer = writer(write_obj)
        # read each row of the input csv file as list
        for row in csv_reader:
            # append the headers and values from checks in add_to_row
            transform_row(row, csv_reader.line_num)
            # add the updated row to the output file
            csv_writer.writerow(row)

# let them know it is doing something
print('Analyzing input file . . .')
# actually add the data to the new csv here
add_column_in_csv(input_file, output_file, add_to_row)
# it is done
print('Done analyzing file. Output created: ' + str(output_file))

标签: pythoncsv

解决方案


推荐阅读