使用 python 覆盖 csv 文件中的第一列和最后一列
Overwrite the first and last column in csv file using python
我不熟悉使用 CSV 模块进行数据处理。我有输入文件 并使用此代码`
import csv
path1 = "C:\Users\apple\Downloads\Challenge\raw\charity.a.data"
csv_file_path = "C:\Users\apple\Downloads\Challenge\raw\output.csv.bak"
with open(path1, 'r') as in_file:
in_file.__next__()
stripped = (line.strip() for line in in_file)
lines = (line.split(":$%:") for line in stripped if line)
with open(csv_file_path, 'w') as out_file:
writer = csv.writer(out_file)
writer.writerow(('id', 'donor_id','last_name','first_name','year','city','state','postal_code','gift_amount'))
writer.writerows(lines)
`
是否可以删除 csv 文件第一列和最后一列中的 (:)。我希望输出像
请帮助我。
如果您只想删除第一列和最后一列的“:”,这应该可行。请记住,在阅读数据集之前,您的数据集应该 tab
(或逗号以外的其他内容)分隔,因为正如我在您的问题中评论的那样,您的数据集中有逗号“,”。
path1 = '/path/input.csv'
path2 = '/path/output.csv'
with open(path1, 'r') as input, open(path2, 'w') as output:
file = iter(input.readlines())
output.write(next(file))
for row in file:
output.write(row[1:][:-2] + '\n')
更新
因此,在提供您的代码后,我添加了一个小改动,以从初始文件开始执行整个过程。这个想法是一样的。您应该只排除每行的第一个和最后一个字符。所以你应该 line.strip()[1:][:-2]
.
而不是 line.strip()
import csv
path1 = "C:\Users\apple\Downloads\Challenge\raw\charity.a.data"
csv_file_path = "C:\Users\apple\Downloads\Challenge\raw\output.csv.bak"
with open(path1, 'r') as in_file:
in_file.__next__()
stripped = (line.strip()[1:][:-2] for line in in_file)
lines = (line.split(":$%:") for line in stripped if line)
with open(csv_file_path, 'w') as out_file:
writer = csv.writer(out_file)
writer.writerow(('id', 'donor_id','last_name','first_name','year','city','state','postal_code','gift_amount'))
writer.writerows(lines)
我不熟悉使用 CSV 模块进行数据处理。我有输入文件
import csv
path1 = "C:\Users\apple\Downloads\Challenge\raw\charity.a.data"
csv_file_path = "C:\Users\apple\Downloads\Challenge\raw\output.csv.bak"
with open(path1, 'r') as in_file:
in_file.__next__()
stripped = (line.strip() for line in in_file)
lines = (line.split(":$%:") for line in stripped if line)
with open(csv_file_path, 'w') as out_file:
writer = csv.writer(out_file)
writer.writerow(('id', 'donor_id','last_name','first_name','year','city','state','postal_code','gift_amount'))
writer.writerows(lines)
`
是否可以删除 csv 文件第一列和最后一列中的 (:)。我希望输出像
如果您只想删除第一列和最后一列的“:”,这应该可行。请记住,在阅读数据集之前,您的数据集应该 tab
(或逗号以外的其他内容)分隔,因为正如我在您的问题中评论的那样,您的数据集中有逗号“,”。
path1 = '/path/input.csv'
path2 = '/path/output.csv'
with open(path1, 'r') as input, open(path2, 'w') as output:
file = iter(input.readlines())
output.write(next(file))
for row in file:
output.write(row[1:][:-2] + '\n')
更新
因此,在提供您的代码后,我添加了一个小改动,以从初始文件开始执行整个过程。这个想法是一样的。您应该只排除每行的第一个和最后一个字符。所以你应该 line.strip()[1:][:-2]
.
line.strip()
import csv
path1 = "C:\Users\apple\Downloads\Challenge\raw\charity.a.data"
csv_file_path = "C:\Users\apple\Downloads\Challenge\raw\output.csv.bak"
with open(path1, 'r') as in_file:
in_file.__next__()
stripped = (line.strip()[1:][:-2] for line in in_file)
lines = (line.split(":$%:") for line in stripped if line)
with open(csv_file_path, 'w') as out_file:
writer = csv.writer(out_file)
writer.writerow(('id', 'donor_id','last_name','first_name','year','city','state','postal_code','gift_amount'))
writer.writerows(lines)