[英]Python counting the number of occurence in csv file based on dependence and independence list
我想将计数依赖写入 CSV 的最后一列,但是,它只显示在 CSV 的第一列。 我面临的另一个问题是它应该显示每一行的依赖关系,但它只显示一次。 我的目标是在依赖列出现时显示我的数据。它应该显示 0 和 1 而不是仅显示 1。
https://i.stack.imgur.com/NMNG2.png
writer = csv.writer(read_obj)
writer.writerow([countdependence])
添加上面的代码后,它给了我结果
https://i.stack.imgur.com/pA58a.png
dependence=['customer','team','job']
countdependence = 0
with open('importantdata.csv', 'r+',encoding='utf-8', newline='') as read_obj:
for i in read_obj:
for word in dependence:
if word in i.lower():
countdependence += 1
writer = csv.writer(read_obj)
writer.writerow([countdependence])
print(countdependence)
countdependence=0
dependence=['customer','team','job']
countdependence = 0
with open('importantdata.csv', 'r',encoding='utf-8', newline='\n') as read_obj:
# reading by row
for row in read_obj:
# for each word in the dependence array
for word_in_dependence in dependence:
# checks if the word in dependence is in the row
if word_in_dependence in row.lower():
countdependence += 1
print(countdependence)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.