0
匹配比值條件以下我有一個問題,即:我需要幫助!從CSV閱讀使用Python
在社交網絡:
- 任何用戶都可以發佈原創內容
- 任何用戶都可以重新發布的原創內容或另一用戶的轉貼
- 任何用戶可以遵循任何其他用戶
社交網絡數據以表示CSV文件格式如下:
postId, repostId, followers
1, -1, 120
2, 1, 60
3, 1, 30
4, 2, 90
5, 3, 40
6, 4, 10
7, -1, 240
8, 7, 190
9, 7, 50
其中:
- 帖子ID - 帖子的ID或重新發布
- repostId - 已轉載或-1,如果這是內容的ID原來的職位
- 追隨者 - 即發的帖子已
寫程序的用戶的追隨者數量這將分析這個社交網絡輸出任何原始內容已經達到的觀衆人數。例如,對於用戶1和圖7所示,輸出將是:
1:350
7:480
我的動機是從一個單元以讀取,而CSV文件是打開的,那麼如果單元格小於零,那麼我知道這是一個獨特的帖子。然而,這使得部分總和不完整,所以我正在查看行內的指定值是否與條件匹配。
我的解決辦法:
# import csv module
import csv
# ensure path to csv file is apporpriate in temp
with open("filepath", 'rb') as f:
reader = csv.DictReader(f)
rows = [row for row in reader if row['repostId'] <= '0'] # go through each row of repostId, looking for a negative value
# for each iteration of rows, print the number of corresponding followers
for followers in rows:
print followers
什麼侑問題? yoir解決方案有問題嗎?對於用戶1和用戶7,你的意思是「對於帖子1和7以及他們的轉帖? – Goyo