2 回答

TA貢獻1827條經驗 獲得超9個贊
如果我正確理解您的問題;您需要從 2 個輸入文件中讀?。?/p>
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看起來像這樣
添加回答
舉報