gpt4 book ai didi

python - 如何使用 Python 在 csv 文件中搜索 'text' 或 'number' 并且如果存在则仅将第一列和第二列值打印到新的 csv 文件

转载 作者:行者123 更新时间:2023-11-28 22:27:35 46 4
gpt4 key购买 nike

我想使用 Python 执行以下操作。

Step-1: Read a specific third column on a csv file using Python.
Step-2: Create a list with values got from step-1
Step-3: Take the value of index[0], search in csv file, if present print the values of column 1 and 2 only to a new csv file(There are 6 columns). If Not presents just ignore and goto next search.

文件 1.csv:

Country,Location,number,letter,name,pup-name,null
a,ab,1,qw,abcd,test1,3
b,cd,1,df,efgh,test2,4
c,ef,2,er,fgh,test3,5
d,gh,3,sd,sds,test4,
e,ij,5,we,sdrt,test5,
f,kl,6,sc,asdf,test6,
g,mn,7,df,xcxc,test7,
h,op,8,gb,eretet,test8,
i,qr,8,df,hjjh,test9,

为此编写的 Python 脚本:

import csv
import time
from collections import defaultdict

columns = defaultdict(list)

with open('file1.csv') as f:
reader = csv.reader(f)
reader.next()
for row in reader:
for (i,v) in enumerate(row):
columns[i].append(v)
#print(columns[2])

b=(columns[2])
for x in b[:]:
time.sleep(1)
print x

上述脚本的输出:

MacBook-Pro:test_usr$ python csv_file.py 
1
1
2
3
5
6
7
8
8
MacBook-Pro:test_usr$

我可以完成第 1 步和第 2 步。

请指导我执行第 3 步。那就是如何在 csv 文件中搜索文本/字符串,如果存在,如何仅将特定列值提取到新的 csv 文件中?

输出文件应如下所示:

a,ab
b,cd
c,ef
d,gh
e,ij
f,kl
g,mn
h,op
i,qr

注意:搜索字符串将来自另一个 csv 文件。请不要建议直接打印第 1 列和第 2 列的值的直接答案。

最终代码是这样的:

import csv
import time
from collections import defaultdict

columns = defaultdict(list)

with open('file1.csv') as f:
reader = csv.reader(f)
reader.next()
for row in reader:
for (i,v) in enumerate(row):
columns[i].append(v)
b=(columns[2])

for x in b[:]:
with open('file2.csv') as f, open('file3.csv', 'a') as g:
reader = csv.reader(f)
#next(reader, None) # discard the header
writer = csv.writer(g)
for row in reader:
if row[2] == x:
writer.writerow(row[:2])

文件 1.csv:

Country,Location,number,letter,name,pup-name,null
a,ab,1,qw,abcd,test1,3
b,cd,1,df,efgh,test2,4
c,ef,2,er,fgh,test3,5
d,gh,3,sd,sds,test4,
e,ij,5,we,sdrt,test5,
f,kl,6,sc,asdf,test6,
g,mn,7,df,xcxc,test7,
h,op,8,gb,eretet,test8,
i,qr,8,df,hjjh,test9,

文件 2.csv:

count,name,number,Type,status,Config Version,,IP1,port
1,bob,1,TRAFFIC,end,1.2,,1.1.1.1,1
2,john,1,TRAFFIC,end,2.1,,1.1.1.2,2
4,foo,2,TRAFFIC,end,1.1,,1.1.1.3,3
5.333333333,test,3,TRAFFIC,end,3.1,,1.1.1.4,4
6.833333333,raa,5,TRAFFIC,end,5.1,,1.1.1.5,5
8.333333333,kaa,6,TRAFFIC,end,7.1,,1.1.1.6,6
9.833333333,thaa,7,TRAFFIC,end,9.1,,1.1.1.7,7
11.33333333,paa,8,TRAFFIC,end,11.1,,1.1.1.8,8
12.83333333,maa,8,TRAFFIC,end,13.1,,1.1.1.9,9

如果我运行上面的脚本,file3.csv 的输出:

1,bob
2,john
1,bob
2,john
1,bob
2,john
1,bob
2,john
1,bob
2,john
1,bob
2,john
1,bob
2,john
1,bob
2,john
1,bob
2,john
1,bob
2,john
1,bob
2,john
1,bob
2,john
.
.
.

Its goes like this in loop

但是输出应该是这样的:

count,name
1,bob,
2,john,
4,foo,
5.333333333,test,
6.833333333,raa,
8.333333333,kaa,
9.833333333,thaa,
11.33333333,paa,
12.83333333,maa,

最佳答案

我认为您应该重新考虑您的方法。您可以简单地通过遍历 CSV 文件来实现您的目标,而无需创建中间 dictlist...,并且由于您想要使用特定的列,您使用 DictReaderDictWriter

会让您的生活更轻松,代码更易读
import csv
import time

search_string = "whatever"

with open('file1.csv', 'rb') as f, open('file2.csv', 'wb') as g:
reader = csv.DictReader(f)
c1, c2, c3, *_ = reader.fieldnames
writer = csv.DictWriter(g, fieldnames=(c1, c2))
for row in reader:
if row[c3] == search_string:
writer.writerow({c1:row[c1], c2:row[c2]})

请记住,csv 模块将始终返回字符串。如果需要,您必须自己处理数据类型转换(我在上面的表格中省略了它)。

如果您不想使用 DictReader/DictWriter,我想它有点冗长,并且不希望输出文件中有标题:

with open('file1.csv') as f, open('file2.csv', 'w') as g:
reader = csv.reader(f)
next(reader, None) # discard the header
writer = csv.writer(g)
for row in reader:
if row[2] == search_string:
writer.writerow(row[:2])

关于python - 如何使用 Python 在 csv 文件中搜索 'text' 或 'number' 并且如果存在则仅将第一列和第二列值打印到新的 csv 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43986126/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com