2 回答
TA贡献1827条经验 获得超9个赞
如果我正确理解您的问题;您需要从 2 个输入文件中读取:
1 包含您要查找的用户 ID
2 包含与用户相关的项目数据
以这种方式,这样的事情会在文件 2 中找到您在 1 中指定的所有用户,并将它们写出到 result.csv
在 search_for.csv 中指定您的搜索 ID。请记住,这将在您每次运行时重新写入您的 result.csv。
import csv
import sys
import os
inputPatterns = open(os.curdir + '/search_for.csv', 'rt')
# Reader for the IDs (users) you are looking to find (key)
reader = csv.reader(inputPatterns)
ids = []
# reading the IDs you are looking for from search_for.csv
for row in reader:
ids.append(row[0])
inputPatterns.close()
# Let's see if any of the user IDs we are looking for has any project related info
# if so write them to your output CSV
for userID in ids:
# Organization list with names and Company ID and reader
userList = open(os.curdir + '/users.csv', 'rt')
reader = csv.reader(userList)
# This will be the output file
result_f = open(os.curdir + "/" + userID + ".csv", 'w')
w = csv.writer(result_f)
# Writing header information
w.writerow(['Date', 'Prj1_Assigned', 'Prj1_closed', 'Prj2_assigned', 'Prj2_solved'])
# Scanning for projects for user and appending them
for row in reader:
if userID == row[1]:
w.writerow([row[3], row[4], row[5], row[6], row[7]])
result_f.close()
userList.close()
例如,search_for.csv看起来像这样
添加回答
举报