Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将csv文件中的特定数据附加到单独的python列表中_Python_Excel_List_Csv - Fatal编程技术网

将csv文件中的特定数据附加到单独的python列表中

将csv文件中的特定数据附加到单独的python列表中,python,excel,list,csv,Python,Excel,List,Csv,我在试图找出如何将CSV文件中的两组数据分离到python中两个独立的列表中时遇到了一些麻烦 基本上,我在Excel文档中记录了以下格式的游戏分数: ROUND 1 Player 1, 3, Player 4, 6 Player 2, 4, Player 5, 9 Player 3, 6, Player 6, 2 ROUND 2 Player 1, 3, Player 4, 6 Player 2, 4, Player 5, 9 Player 3, 6, Player 6, 2 同样在同一个文件

我在试图找出如何将
CSV
文件中的两组数据分离到python中两个独立的
列表中时遇到了一些麻烦

基本上,我在Excel文档中记录了以下格式的游戏分数:

ROUND 1
Player 1, 3, Player 4, 6
Player 2, 4, Player 5, 9
Player 3, 6, Player 6, 2
ROUND 2
Player 1, 3, Player 4, 6
Player 2, 4, Player 5, 9
Player 3, 6, Player 6, 2
同样在同一个文件中,我有相同格式的“第二轮”的分数,我基本上想用python将第一轮和第二轮的数据存储到两个不同的列表中。我已经设法读取了csv文件并将此数据附加到列表中,但是,它将两组数据附加到同一个列表中,这并不是期望的效果。是否有一种方法可以将csv文件中的数据添加到列表中,直到其显示为“第二轮”,然后开始将数据添加到另一个列表中(如果有意义)

这是我到目前为止的代码,但它没有切换
列表

round1Scores = []
round2Scores = []
with open(roundScores) as csvfile:
     for row in csv.reader(csvfile):
        round1Scores.append(row)

如果您想通过切换
列表来实现这一点,当您到达
第二轮时,请使用
变量

round1Scores = []
round2Scores = []
round = 1
with open(roundScores) as csvfile:
     for row in csv.reader(csvfile):
         if "ROUND 2" in row:
             round = 2
         if round == 1:
             round1Scores.append(row)
         elif round == 2:
             round2Scores.append(row)

欢迎来到StackOverflow。我很难理解你的问题,因为你没有向我们展示任何样本数据,我也不理解你的模板。请向我们展示示例数据文件、所需输出、您现在获得的输出以及输出错误的原因。请阅读并遵循。对不起,我将尝试澄清数据集。因此,我有一个excel文件,第一列包含不同的玩家,如“玩家1”、“玩家2”、“玩家3”等。第二列包含玩家的分数,每个玩家的分数将与玩家在同一行。然后在接下来的两列中重复此格式,以表示对方球员及其得分。然后在这下面,我又做了同样的事情来计算“第二轮”的分数。我基本上想把第一轮分数读入一个列表,然后把第二轮分数读入第二个列表基本上我想问的是,有没有一种方法可以把第一轮分数中的数据读入python列表,当文件被读取时,它最终会命中一个包含“第二轮”的单元格,此时,我希望停止向第一个列表添加数据,并开始向第二个列表添加数据。这有意义吗?太棒了,它能工作!我很感激这位帮助人,我没有意识到这就像添加一个变量并使用“If Round 2 in row”语句一样简单:@SeanCampbell请
accept
up vote
,这就是这个网站的工作原理!很高兴我能帮忙!