python批量读取txt并写入CSV
时间: 2023-09-10 16:10:50 浏览: 128
Sure, I can definitely help you with that. Here's some sample code that can help you achieve this:
```
import csv
# Open the CSV file for writing
with open('output.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
# Open the text file containing data
with open('input.txt', 'r') as infile:
for line in infile:
# Split the line by tab delimiter
data = line.strip().split('\t')
# Write row to CSV file
writer.writerow(data)
```
In this code, we first open a CSV file for writing using `csv.writer`. Then, we open the input text file using `open` and loop through each line. We split each line into a list of values using the `split` method and the tab delimiter. Finally, we write the list as a row in the CSV file using `writer.writerow`.
阅读全文