如果您想在python中写入CSV文件并添加一列数据,可以使用csv库。
首先,您需要打开CSV文件,并将其设置为写入模式。然后,您可以使用writer.writerow()方法向CSV文件写入一行数据,该方法接受一个列表作为参数,其中包含该行中的所有数据。
import csv
Open the file in write mode
with open("example.csv", "w", newline="") as file: writer = csv.writer(file) # Write the first row (header) writer.writerow(["Column 1", "Column 2", "Column 3"]) # Write the data writer.writerow(["Data 1", "Data 2", "Data 3"])
这样,您就可以在第三列中写入“Data 3”的数据。