Python基于依赖性和独立性列表计算csv文件中的出现次数

2024-10-01 22:32:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我想将计数相关性写入CSV的最后一列,但是,它只显示在CSV的第一列。我面临的另一个问题是,它应该显示每一行的相关性,但它只显示一次。 我的目标是在dependence列出现时显示我的数据。它应该显示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

Tags: csvinhttpscomobjforreadpng
1条回答
网友
1楼 · 发布于 2024-10-01 22:32:21
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)

相关问题 更多 >

    热门问题