用新行读取CSV文件

发布于 2025-01-17 08:59:52 字数 522 浏览 1 评论 0原文

我想读取我创建的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

您可以在行上迭代并以所需的格式打印每一行:

# For each row
for row in rows:
    # Make sure the row is not empty
    if row:
        # Print the row
        print(row[0], row[1])

或者,您可以使用列表理解将其全部保存为一个字符串,以将其作为一个变量:

# For each row,
# if the row is not empty
# format it into a string.
# Join all the resulting strings together by a new line.
my_data_string = "\n".join([f"{row[0]} {row[1]}" for row in rows if row])

You can iterate over your rows and print each row in the format that you'd like:

# For each row
for row in rows:
    # Make sure the row is not empty
    if row:
        # Print the row
        print(row[0], row[1])

Or alternatively, you could use list comprehension to save it all as a string to a variable:

# For each row,
# if the row is not empty
# format it into a string.
# Join all the resulting strings together by a new line.
my_data_string = "\n".join([f"{row[0]} {row[1]}" for row in rows if row])
残龙傲雪 2025-01-24 08:59:52

此方法将以您请求的格式打印,并对行进行编号。

import pandas as pd

data = pd.read_csv('test.csv', header = None, names = ['Team Name', 'Number', 'Score'])

print(data)

输出:

      Team Name  Number  Score
0     Team One       23    NaN
1     Team Two       102   NaN
2     Team Three     44    NaN
3     Team Four      40    NaN

This method will print in the format you requested and number your rows as well.

import pandas as pd

data = pd.read_csv('test.csv', header = None, names = ['Team Name', 'Number', 'Score'])

print(data)

Output:

      Team Name  Number  Score
0     Team One       23    NaN
1     Team Two       102   NaN
2     Team Three     44    NaN
3     Team Four      40    NaN
遥远的她 2025-01-24 08:59:52

现在还有最后一个问题;

这是输出:

'''

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文