读取文件并计数python列中的单词

时间:2019-07-21 11:01:45

标签: python-3.x

我是python的新手,我需要一些帮助来读取文件并计算列中的单词。

我有2个数据文件,分别是category.csv和data.csv。

category.csv:

CATEGORY
Technology
Furniture
Office Supplies

下面是data.csv

CATEGORY
Technology
Furniture
Technology
Furniture
Office Supplies

首先,我想在category.csv中选择“技术”并将其与data.cvs匹配,然后,它将开始计算“技术”在data.cvs中出现了多少次。

import csv  # import csv file
filePath1 = "category.csv"
filePath2 = "data.csv"
with open(filePath1) as csvfile1:  # open category file
    with open(filePath2) as csvfile2:  # open data file
        reader1 = csv.DictReader(csvfile1)  # dictread file
        reader2 = csv.DictReader(csvfile2)  # dictread file
        for row1 in reader1:  # read all row in data file
            for row2 in reader2:
                for row1['CATEGORY'] in row2['CATEGORY']:
                    total_tech = row2['CATEGORY'].count('Technology')
                    total_furn = row2['CATEGORY'].count('Furniture')
                    total_offi = row2['CATEGORY'].count('Office Supplies')
                    print("=============================================================================")
                    print("Display category average stock level")
                    print("=============================================================================")
                    print( "Technology      :", total_tech)
                    print("Furniture       :", total_furn)
                    print("Office Supplies :", total_offi)
                    print( "=============================================================================")

但是我无法用上面的代码来计算,有人可以帮助我吗?非常感谢。

1 个答案:

答案 0 :(得分:0)

这是解决方案-

import csv  # import csv file

filePath1 = "category.csv"
filePath2 = "data.csv"

categories = {}
with open(filePath1) as csvfile:  # open category file
    reader = csv.DictReader(csvfile)  # dictread file
    for row in reader:  # Create a dictionary map of all the categories, and initialise count to 0
        categories[row["CATEGORY"]] = 0

with open(filePath2) as csvfile:  # open data file
    reader = csv.DictReader(csvfile)  # dictread file
    for row in reader:
        categories[row["CATEGORY"]] += 1 # For every item in data file, increment the count of the category

print("=============================================================================")
print("Display category average stock level")
print("=============================================================================")
for key, value in categories.items():
    print("{:<20} :{:>4}".format(key, value))
print("=============================================================================")

输出是这样的-

=============================================================================
Display category average stock level
=============================================================================
Technology           :   2
Office Supplies      :   1
Furniture            :   2
=============================================================================