回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我正在创建一个boardgame排名系统,我有一个.CSV列表,上面列出了在boardgame之夜击败其他人的人。打败一个打败了另一个人的人算是胜利。我需要刮这个CSV文件,找到那些被胜利者打败的人打败的人,然后附加数据并排序。你知道吗</p>
<p>我可以打开、创建、获取数据并写入文件,但当我尝试编写各种版本的代码时,我似乎无法获得正确的输出。你知道吗</p>
<pre><code> import csv
import collections
#get the contents of the input.csv file
WINLOSE = {}
with open('input.csv') as f2:
for line in f2:
winners,losers = line.strip().split(',')
WINLOSE[winners] = losers
new_items = set()
RESULTS = collections.namedtuple('RESULTS', ['winners', 'losers'])
#Write to the output file.
with open('output.csv', 'w') as f1:
writer = csv.DictWriter(winners, losers)
#pseudo code -- if any name in the winner cell appears in the
#loser cell,
#copy all of the losers associated with that cell to the people
#who beat that
#cell
if cell.losers = any-cell.winners:
append the losers associated with the winners cell
for row in new_items:
writer.writerow(row._asdict())
</code></pre>
<p>输入csv如下所示:</p>
<pre><code> Winners,Losers
John,Amanda
Mark,Eddy
Amanda,Chad
Becky,Michael
Michael,Steve
Eddy,Fred
Michael, Stuart
Edwardo, Patricia
Michael, Buzz
Mark, Charlie
Amanda, Brandon
Brandon, Dirk
</code></pre>
<p>输出csv应如下所示:</p>
<pre><code> Winners,Losers
John,Amanda
John,Chad
John, Brandon
John, Dirk
Mark,Eddy
Mark,Fred
Mark, Charlie
Amanda,Chad
Becky,Michael
Becky,Steve
Michael,Steve
Michael, Stuart
Michael, Buzz
Eddy,Fred
Edwardo, Patricia
Amanda, Brandon
Brandon, Dirk
</code></pre>
<p>例如,johnbeatamanda和amandabeatchad,因此我们需要添加johnbeatchad的条目。你知道吗</p>