python 写csv代码
时间: 2023-10-14 11:15:55 浏览: 58
python将数据写入csv文件
5星 · 资源好评率100%
Sure, I can help you with that! Here's a sample code snippet for writing to a CSV file in Python:
```python
import csv
# Open the CSV file for writing
with open('example.csv', 'w', newline='') as file:
writer = csv.writer(file)
# Write the header row
writer.writerow(['Name', 'Age', 'City'])
# Write some data rows
writer.writerow(['Alice', 25, 'New York'])
writer.writerow(['Bob', 30, 'San Francisco'])
writer.writerow(['Charlie', 35, 'London'])
```
This code creates a new CSV file called "example.csv" and writes some data to it. You can customize the file name and data as needed. Let me know if you have any other questions!
阅读全文