2 回答

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超9個(gè)贊
如果我正確理解您的問(wèn)題;您需要從 2 個(gè)輸入文件中讀取:
1 包含您要查找的用戶(hù) ID
2 包含與用戶(hù)相關(guān)的項(xiàng)目數(shù)據(jù)
以這種方式,這樣的事情會(huì)在文件 2 中找到您在 1 中指定的所有用戶(hù),并將它們寫(xiě)出到 result.csv
在 search_for.csv 中指定您的搜索 ID。請(qǐng)記住,這將在您每次運(yùn)行時(shí)重新寫(xiě)入您的 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看起來(lái)像這樣
添加回答
舉報(bào)