【发布时间】:2020-06-10 12:04:24
【问题描述】:
输入的 CSV 文件如下。
Elements,FileID,Loadcase,Step,Layer,CornerID,E-Strain components IP:E11
41,2,1,0,SectionPoint 1,0,0
41,2,1,0,SectionPoint 1,5,0
41,2,1,0,SectionPoint 1,35,0
在此文件中,我想删除整个CornerID 列并编写一个新的 CSV 文件。
注意:脚本应该使用标题名称并删除该列。不像列索引级别删除的方式。因为像这样我有多个文件,在某些文件中列号不会相同。
谁能帮我获取python代码?
导入 csv
inputf = "D:\PyLe\csvinput\Report_E-应变组件 IP_E11_2_1.csv" outfile = "D:\PyLe\csvinput\Report_E-Strain 组件 IP_E11_2_1_new.csv"
import csv
with open(inputf, newline='') as csvfile:
outfile1 = open (outfile , 'w')
reader = csv.DictReader(csvfile)
for row in reader:
del (row['CornerID'])
spamwriter = csv.writer(outfile1, delimiter=' ',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
spamwriter.writerow(row)
print (type(row))
此代码正在读取 csv 文件并删除该列。但它没有用 csv 格式写出新文件。
【问题讨论】:
-
df.drop(['CornerID'], axis=1, inplace=True)然后保存为 csv 怎么样? -
请给我完整的代码..:)
标签: python python-3.x csv