python - 如何提高 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))
解决方案
推荐阅读
- python - Selenium Python 为产品和颜色抓取网站
- python - 将 GridSearchCV 与完整数据 VS 与仅包含统计显着变量的数据一起使用背后的直觉
- reactjs - 在反应中在字符串文字内插入新行
- python - 如何正确设置 pip 以安装要求并运行可执行文件?
- internet-explorer - 在 IE 中垂直压缩的引导轮播图像
- scala - 如何更改 ScalaFX 中的 LineChart XAxis 下限/上限?
- python - Python 3 re.sub 的奇怪行为
- r - R - 组内的聚类(K-means)
- image - 如何在颤动中将图标转换为图像?
- java - 在 SpringBoot 中实现简单的异步方法调用时遇到问题