我在弄清楚该个人项目的具体位置时遇到了麻烦,我希望这个社区可以帮助我创建一个Python脚本来处理这些数据。
我有一个CSV文件,其中包含动物救助时喂给狗的饭食的清单,并与狗窝号关联:
源CSV-mealsandtreats.csv
blank_column,Kennel_Number,Species,Food,Meal_ID
,1,Dog,Meal,11.2
,5,Dog,Meal,45.2
,3,Dog,Meal,21.4
,4,Dog,Meal,17
,2,Dog,Meal,11.2
,4,Dog,Meal,21.4
,6,Dog,Meal,17
,2,Dog,Meal,45.2
我还有第二个CSV文件,该文件提供了一个密钥,可将餐点映射到餐点附带的食物:
用于处理密钥的餐点-MealsToTreatsKey.csv
Meals_fed,Treats_fed
10.1,2.4
11.2,2.4
13.5,3
15.6,3.2
17,3.2
20.1,5.1
21.4,5.2
35.7,7.7
45.2,7.9
我需要接受从表1交付的每种餐食类型(例如,删除重复的条目),找到相关的零食类型,然后在每次为特定的狗窝提供零食时创建一个单独的条目。最终结果应如下所示:
结果CSV-mealsandtreats.csv
blank_column,Kennel_Number,Species,Food,Meal_ID
,1,Dog,Meal,11.2
,5,Dog,Meal,45.2
,3,Dog,Meal,21.4
,4,Dog,Meal,17
,2,Dog,Meal,11.2
,4,Dog,Meal,21.4
,6,Dog,Meal,17
,2,Dog,Meal,45.2
,1,Dog,Treat,2.4
,5,Dog,Treat,7.9
,3,Dog,Treat,5.2
,4,Dog,Treat,3.2
,1,Dog,Treat,2.4
,4,Dog,Treat,5.2
宁愿使用csv模块而不是Pandas来执行此操作,但如有必要,我愿意使用Pandas。
到目前为止,我仅打开CSV就有一些代码,但是我真的对下一步的工作很执着:
import csv
with open('./meals/results/foodToTreats.csv', 'r') as t1,
open('./results/food.csv', 'r') as t2:
key = t1.readlines()
map = t2.readlines()
with open('./results/food.csv', 'w') as outFileF:
for line in map:
if line not in key:
outFileF.write(line)
with open('./results/foodandtreats.csv', 'w') as outFileFT:
for line in map:
if line not in key:
outFileFT.write(line)
因此,基本上,我只需要获取第二张表中的每个请客条目,在第一张表中搜索匹配的相关食品条目,查找与该条目相关的狗窝编号,然后将其写入第一张表中。
用伪代码尽我最大的努力,例如:
for x in column 0,y:
y,1 = Z
food = x
treat = y
kennel_number = z
when x,z:
writerows('', {'kennel_number"}, 'species', '{food/treat}',
{'meal_id"})
更新:这是我正在使用的确切代码,这要感谢@wwii。看到一个小错误:
import csv
import collections
treats = {}
with open('mealsToTreatsKey.csv') as f2:
for line in f2:
meal,treat = line.strip().split(',')
treats[meal] = treat
new_items = set()
Treat = collections.namedtuple('Treat', ['blank_column','Kennel_Number','Species','Food','Meal_ID'])
with open('foodandtreats.csv') as f1:
reader = csv.DictReader(f1)
for row in reader:
row['Food'] = 'Treat'
row['Meal_ID'] = treats[row['Meal_ID']]
new_items.add(Treat(**row))
fieldnames = reader.fieldnames
with open('foodandtreats.csv', 'a') as f1:
writer = csv.DictWriter(f1, fieldnames)
for row in new_items:
writer.writerow(row._asdict())
除一个小错误外,这非常有效。所写的第一行新记录不是一行一行开始的: enter image description here
答案 0 :(得分:1)
制作一本字典,将饭菜映射成美食
treats = {}
with open(treatfile) as f2:
for line in f2:
meal,treat = line.strip().split(',')
treats[meal] = treat
遍历进餐文件并创建一组新条目。对新项目使用namedtuple。
import collections
new_items = set()
Treat = collections.namedtuple('Treat', ['blank_column','Kennel_Number','Species','Food','Meal_ID'])
with open(mealfile) as f1:
reader = csv.DictReader(f1)
for row in reader:
row['Food'] = 'Treat'
row['Meal_ID'] = treats[row['Meal_ID']]
new_items.add(Treat(**row))
fieldnames = reader.fieldnames
(再次)打开膳食文件以添加并写入新条目
with open(mealfile, 'a') as f1:
writer = csv.DictWriter(f1, fieldnames)
for row in new_items:
writer.writerow(row._asdict())
如果餐文件没有以换行符结尾,则需要在写入新的treat
行之前添加一个。由于您可以控制文件,因此只需确保文件始终以空行结尾。