简体   繁体   English

Python根据依赖和独立列表统计csv文件中出现的次数

[英]Python counting the number of occurence in csv file based on dependence and independence list

I want to write the count dependence into the last column of CSV, however, it only displays at the first column of the CSV.我想将计数依赖写入 CSV 的最后一列,但是,它只显示在 CSV 的第一列。 Another problem I faced is that it is supposed to display the occurrence of dependence for each row but it only displays one time only.我面临的另一个问题是它应该显示每一行的依赖关系,但它只显示一次。 My goal is to display my data at the occurrence of the dependence column.It is supposed to display 0 and 1 instead of 1 only.我的目标是在依赖列出现时显示我的数据。它应该显示 0 和 1 而不是仅显示 1。

https://i.stack.imgur.com/NMNG2.png https://i.stack.imgur.com/NMNG2.png

writer = csv.writer(read_obj)
writer.writerow([countdependence])

After I added the above code it gave me the result of添加上面的代码后,它给了我结果

https://i.stack.imgur.com/pA58a.png 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM