![](/img/trans.png)
[英]How to find row number of a particular value with known column number in csv file through python
[英]How to update csv values given their row and column numbers are known with python
我有一些代码可以查看 a.csv 文件的行,并检查这些行是否包含来自 another.csv 文件的特定列的任何值。
这些文件如下所示:
查找文件:
0: TextExclude, text, other
1: aa, , x , y
2: bb, , x , y
3: cc, , x , y
我要在其中找到这些值的文件:
0: x, longtext, exclude
1: x, helloaa, 0
2: x, testaa, 0
3: x, testcc, 0
4: x, no, 0
5: x, aabb, 0
我的代码的 output 应该在除第 4 行之外的每一行中将“包含”列的值从 0 更改为 1,从而产生预期的 output Z628CB5675FF524F3E719B7AA28 表:
0: x, longtext, exclude
1: x, helloaa, 1
2: x, testaa, 1
3: x, testcc, 1
4: x, no, 0
5: x, aabb, 1
由于我的代码可以 output 找到匹配的行号并且列号已经定义,我想知道解决这个问题并相应地更新 .csv 文件的最佳方法是什么?
这是我的代码:
import pandas
findlist = []
linecount=0
with open('lookup.csv', 'r') as f:
column_names = ["TextExclude", "Exclusion", "Filename"]
r = pandas.read_csv(f, names=column_names)
findlist = r.TextExclude.to_list()
with open('datafile.csv', 'r') as f:
# Skip the first line
f.readline()
for line in f:
linecount = linecount +1
if any(listelement in line for listelement in findlist):
print(line)
您应该使用 pandas 解析 datafile.csv,而不仅仅是将其作为纯文本文件读取。 这样,您可以将搜索隔离到正确的列,并更轻松地更新第三列。
import pandas
findlist = []
linecount=0
with open('lookup.csv', 'r') as f:
column_names = ["TextExclude", "Exclusion", "Filename"]
r = pandas.read_csv(f, names=column_names)
findlist = r.TextExclude.to_list()
with open('datafile.csv', 'r') as f:
df = pandas.read_csv(f)
for ri, row in df.iterrows():
if any(x in row[1] for x in findlist):
row[2] = "1"
print(df)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.