有4个csv文件,tt_0c.csv, tt_100c.csv,ss_0c.csv,ss_100c.csv。生成一个新的csv,第一列名叫tt,0c和tt_0c.csv的第二列为列元素。依次类推
时间: 2024-03-01 14:04:21 浏览: 47
对于csv文件的操作
好的,我可以帮你写一个Python脚本来完成这个任务。你可以将以下代码保存为一个.py文件并运行。
```python
import csv
# 读取tt_0c.csv文件
with open('tt_0c.csv', 'r') as file:
reader = csv.reader(file)
tt_0c = [row[1] for row in reader]
# 读取tt_100c.csv文件
with open('tt_100c.csv', 'r') as file:
reader = csv.reader(file)
tt_100c = [row[1] for row in reader]
# 读取ss_0c.csv文件
with open('ss_0c.csv', 'r') as file:
reader = csv.reader(file)
ss_0c = [row[1] for row in reader]
# 读取ss_100c.csv文件
with open('ss_100c.csv', 'r') as file:
reader = csv.reader(file)
ss_100c = [row[1] for row in reader]
# 将数据写入新的csv文件
with open('new.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['tt', ''])
writer.writerow(['0c', tt_0c])
writer.writerow(['100c', tt_100c])
writer.writerow(['ss', ''])
writer.writerow(['0c', ss_0c])
writer.writerow(['100c', ss_100c])
```
这个脚本先分别读取四个csv文件的第二列数据,然后将它们按照要求写入一个新的csv文件中。你只需要将脚本中的文件名替换为你实际使用的文件名即可。
阅读全文