要打开csv文件,逐行读取文件,执行base64_decode()操作,并将解码后的数据写入新文件,可以按照以下步骤进行:
import csv
import base64
with open('input.csv', 'r') as csvfile, open('output.csv', 'w') as outfile:
reader = csv.reader(csvfile)
writer = csv.writer(outfile)
for row in reader:
decoded_row = []
for item in row:
decoded_item = base64.b64decode(item).decode('utf-8')
decoded_row.append(decoded_item)
writer.writerow(decoded_row)
完整的代码示例如下:
import csv
import base64
with open('input.csv', 'r') as csvfile, open('output.csv', 'w') as outfile:
reader = csv.reader(csvfile)
writer = csv.writer(outfile)
for row in reader:
decoded_row = []
for item in row:
decoded_item = base64.b64decode(item).decode('utf-8')
decoded_row.append(decoded_item)
writer.writerow(decoded_row)
以上代码可以实现打开csv文件,逐行读取文件,执行base64解码操作,并将解码后的数据写入新文件。请注意,代码中的'input.csv'和'output.csv'分别表示输入文件和输出文件的路径,可以根据实际情况进行修改。
腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云