【发布时间】:2026-02-02 01:55:08
【问题描述】:
我有一个 CSV 文件 (test.csv):
"test1", "https://test.com/file1.jpg"
"test/23", "https://test.com/file1.jpg"
"test34", "https://test.com/file78.jpg"
"test/23", "https://test.com/file1.jpg"
我需要使用 python 将第一列中的“/”替换为“_”。 我确实看过 pandas 和其他示例,但它们只显示了如何替换整个 csv 文件。
我使用 csv 文件通过此脚本下载图像列表:
from urllib.request import urlretrieve
from datetime import datetime
import csv
with open ('test.csv') as images:
images = csv.reader(images)
img_count = 0
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
for image in images:
urlretrieve(image[1],
'{}.jpg'.format(image[1]+'_'+image[0]))
img_count += 1
print("Current Time =", current_time)
print(img_count)
问题是脚本失败,因为它不能在文件名中使用 /。
【问题讨论】:
-
您可以通过多种方式解决此问题。请包括您想要/需要使用的技术,并展示您已经完成的代码示例。
-
我刚刚编辑了我的问题。我希望它有意义
-
请注意,修改字符串与读取 csv 文件无关——它们是两个独立的任务。也许这就是您的问题被否决的原因。