Python 3.x 在python中读取文件并计算列中的单词数
我是python新手,我需要一些关于读取文件和计算列中单词的帮助 我有两个数据文件,分别是category.csv和data.csv category.csv:Python 3.x 在python中读取文件并计算列中的单词数,python-3.x,Python 3.x,我是python新手,我需要一些关于读取文件和计算列中单词的帮助 我有两个数据文件,分别是category.csv和data.csv category.csv: CATEGORY Technology Furniture Office Supplies 下面是data.csv CATEGORY Technology Furniture Technology Furniture Office Supplies 首先,我想在category.csv中选择“Technology”,并将其与data
CATEGORY
Technology
Furniture
Office Supplies
下面是data.csv
CATEGORY
Technology
Furniture
Technology
Furniture
Office Supplies
首先,我想在category.csv中选择“Technology”,并将其与data.cvs匹配,然后,它将开始计算“Technology”在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( "=============================================================================")
但我没能用上面的代码数出来,有人能帮我吗?非常感谢。这是解决方案-
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("=============================================================================")
注:已将OP的格式改为4个空格。
=============================================================================
Display category average stock level
=============================================================================
Technology : 2
Office Supplies : 1
Furniture : 2
=============================================================================