用新行读取CSV文件
我想读取我创建的 CSV 文件,并将其打印在新行上:
代码
rows = []
with open("test.csv", 'r') as file:
csvreader = csv.reader(file)
header = next(csvreader)
for row in csvreader:
rows.append(row)
print(header)
print(rows)
这是我得到的输出的
['TeamName', 'MCount']
[[], ['team One', '23'], ['Team Two', '102'], ['Team Three', '44'], ['Team Four', '40']]
是这样的...我希望它看起来像这样:
Team One 23
Team Two 102
Team Three 44
Team Four 40
I want to read the CSV file that i have created, and have it printed on new lines:
this is the code
rows = []
with open("test.csv", 'r') as file:
csvreader = csv.reader(file)
header = next(csvreader)
for row in csvreader:
rows.append(row)
print(header)
print(rows)
the output I get is this...
['TeamName', 'MCount']
[[], ['team One', '23'], ['Team Two', '102'], ['Team Three', '44'], ['Team Four', '40']]
I want it to look like this:
Team One 23
Team Two 102
Team Three 44
Team Four 40
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在行上迭代并以所需的格式打印每一行:
或者,您可以使用列表理解将其全部保存为一个字符串,以将其作为一个变量:
You can iterate over your rows and print each row in the format that you'd like:
Or alternatively, you could use list comprehension to save it all as a string to a variable:
此方法将以您请求的格式打印,并对行进行编号。
输出:
This method will print in the format you requested and number your rows as well.
Output:
现在还有最后一个问题;
这是输出:
'''
0 1 2
0 TeamName MCount Score
1 Team One 23 NaN
2 Team Two 234 NaN
3 Team Three 3 NaN
'''
上面的数字我不想要
Now have one final problem;
this is the output:
'''
0 1 2
0 TeamName MCount Score
1 Team One 23 NaN
2 Team Two 234 NaN
3 Team Three 3 NaN
'''
the numbers on top i do not want