我有一个包含以下内容的CSV文件:
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
1,10,19,,,,,,,,,,,,,
2,11,20,,,,,,,,,,,,,
3,12,21,,,,,,,,,,,,,
4,13,22,,,,,,,,,,,,,
5,14,23,,,,,,,,,,,,,
6,15,24,,,,,,,,,,,,,
7,16,25,,,,,,,,,,,,,
8,17,26,,,,,,,,,,,,,
9,18,27,,,,,,,,,,,,,
我需要通过索引删除一些列。
我尝试了以下代码,它没有按预期返回结果,有人帮我一样。
import csv
def read():
with open("test.csv", "rb") as fp_in, open("newfile.csv", "wb") as fp_out:
reader = csv.reader(fp_in, delimiter=",")
writer = csv.writer(fp_out, delimiter=",")
col_list = [0,1,2,3,4,5,6,8]
for row in reader:
for col_item in col_list:
print(col_item)
del row[int(col_item)]
writer.writerow(row)
read()
结果返回:
1,3,5,7,9,11,13,14
10,,,,,,,
11,,,,,,,
12,,,,,,,
13,,,,,,,
14,,,,,,,
15,,,,,,,
16,,,,,,,
17,,,,,,,
18,,,,,,,
问题是因为读者对于每次迭代总是相同的,我需要删除列表中的所有列。
有人帮我一样。
所需的输出应该是这样的:
7,9,10,11,12,13,14,15
,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,,
.
.
.
.
准确地说,我只想删除提到的列及其值。
编辑:
一些明显的例子。
def read():
with open("test.csv", "rb") as fp_in, open("newfile.csv", "wb") as fp_out:
reader = csv.reader(fp_in, delimiter=",")
writer = csv.writer(fp_out, delimiter=",")
col_list = [0,2]
for row in reader:
for col_item in col_list:
print(col_item)
del row[int(col_item)]
writer.writerow(row)
read()
我得到的输出:
1,2,4
v,d,q
c,s,a
s,d,d
f,x,c
预期:
1,3,4
v,s,q
c,d,a
s,f,d
f,a,c
答案 0 :(得分:1)
问题是你在col_list的每次迭代中改变了行。
这应该有用;使用列表推导来制作没有col_list索引的行的副本。
def read():
with open("test.csv", "r") as fp_in, open("newfile.csv", "w") as fp_out:
reader = csv.reader(fp_in, delimiter=",")
writer = csv.writer(fp_out, delimiter=",")
col_list = [0,1,2,3,4,5,6,8]
for row in reader:
output = [v for (i,v) in enumerate(row) if i not in col_list]
writer.writerow(output)
将以下内容写入newfile.csv:
7,9,10,11,12,13,14,15
,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,,
答案 1 :(得分:0)
你可以这样做。
假设您的输入文件名为input.txt
with open('input.txt', 'r') as f:
data = [k.split(',') for k in f.read().splitlines()]
for k in data:
print(k[7] + ',' + ','.join(k[9:]))
如果您想将结果保存到文件中(例如final_file.txt
调用),您可以执行以下操作:
with open("final_file.txt", 'a') as f:
for k in data:
f.write(k[7] + ',' + ','.join(k[9:]) + '\n')
输出:
7,9,10,11,12,13,14,15
,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,,
答案 2 :(得分:0)
您可以尝试使用pandas到drop
个特定列,然后写入csv文件:
import pandas as pd
df = pd.read_csv('test.csv')
df = df.drop(['0','1','2','3','4','5','6','8'], axis=1)
df.to_csv('newfile.csv',index=False)
newfile.csv
将是:
7,9,10,11,12,13,14,15
,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,,
答案 3 :(得分:0)