要在Python中向CSV文件中新增一列數據,可以按照以下步驟進行:
import csv
with open('file.csv', 'r') as file:
reader = csv.reader(file)
data = list(reader)
for row in data:
# 添加新的列數據
new_column_data = 'new_data'
row.append(new_column_data)
with open('file.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerows(data)
這樣就可以成功向CSV文件中新增一列數據。