将数组保存为CSV文件而不将其转换为字符串可以通过以下步骤实现:
import csv
data = [
['Name', 'Age', 'City'],
['John', 28, 'New York'],
['Alice', 32, 'London'],
['Bob', 45, 'Paris']
]
filename = 'data.csv'
writerows()
函数将数组数据写入CSV文件:with open(filename, 'w', newline='') as file:
writer = csv.writer(file)
writer.writerows(data)
import csv
data = [
['Name', 'Age', 'City'],
['John', 28, 'New York'],
['Alice', 32, 'London'],
['Bob', 45, 'Paris']
]
filename = 'data.csv'
with open(filename, 'w', newline='') as file:
writer = csv.writer(file)
writer.writerows(data)
这样,数组数据就会以CSV格式保存在指定的文件中,而不会被转换为字符串。
领取专属 10元无门槛券
手把手带您无忧上云