首页 > 解决方案 > 无法正确打印 CSV 列

问题描述

我是编程新手,并试图通过做一些在线教程来补充我的学习。今天,我开始使用一个似乎很容易理解的教程来处理 CSV 文件,但我遇到了一个无关紧要的问题,但我无法弄清楚,这让我很沮丧,哈哈。我花了大约两个小时在谷歌上搜索和测试东西,但我只是不够精明,不知道下一步该尝试什么。请帮忙!哈哈。

这是有问题的代码:

# importing the csv module
import csv
# csv filename

filename = r'C:\Users\XXX\Documents\AAPL.csv'

# initialize the titles and row list
fields = []
rows = []

# read the csv file
with open(filename, 'r') as csvfile:
    # create the csv reader object
    csvreader = csv.reader(csvfile)

    # extract field names through the first row
    fields = next(csvreader)

    # extract each data row one by one
    for row in csvreader:
        rows.append(row)

    # get total number of rows
    print("total no. of rows: %d"%(csvreader.line_num))

# print the field names
print("Field names are: " + ", ".join(field for field in fields))

# print the first 5 rows of data
print("\nFirst 5 rows are:\n")
for row in rows[:5]:
    #parse each column of a row
    for col in row:
       print("%10s"%col),
    print("\n")

该教程实际上是为 Python 2.X 编写的,因此我找到了 3.6 的更新格式并将最后一条语句更改为:

for col in row:
   print('{:>10}'.format(col))
print("\n")

无论采用哪种方式编写,结果都会以这种格式出现:

First 5 rows are:

2013-09-18
 66.168571
 66.621429
 65.808571
 66.382858
 60.492519
 114215500
...

而不是教程中显示的预期柱状格式。

当我在某处读到您需要为每个项目设置格式时,我想我终于找到了解决方案,所以我尝试了:

for col in row:
   print('{:>10} {:>10} {:>10} {:>10} {:>10} {:>10} {:>10}'.format(*col))
print("\n")

以便每列都有格式,但是这似乎为字段中的每个字母创建了一个列,例如:

 2          0          1          3          -          0          9

CSV 只是一个 AAPL 股票价格的文件——如果你想创建一个用于测试的 CSV,这里是前 9 行数据:

Date,Open,High,Low,Close,Adj Close,Volume
2013-09-18,66.168571,66.621429,65.808571,66.382858,60.492519,114215500
2013-09-19,67.242859,67.975716,67.035713,67.471428,61.484497,101135300
2013-09-20,68.285713,68.364288,66.571426,66.772858,60.847912,174825700
2013-09-23,70.871429,70.987144,68.942856,70.091431,63.872025,190526700
2013-09-24,70.697144,70.781425,69.688568,69.871429,63.671543,91086100
2013-09-25,69.885712,69.948570,68.775711,68.790001,62.686062,79239300
2013-09-26,69.428574,69.794289,69.128571,69.459999,63.296616,59305400
2013-09-27,69.111427,69.238571,68.674286,68.964287,62.844891,57010100
2013-09-30,68.178574,68.808571,67.772858,68.107140,62.063782,65039100

标签: pythonpython-3.xcsvformatting

解决方案


# importing csv module 
import csv 

# csv file name 
filename =  r'C:\Users\XXX\Documents\AAPL.csv'


# initialize the titles and row list
fields = []
rows = []

# read the csv file
with open(filename, 'r') as csvfile:
    # create the csv reader object
    csvreader = csv.reader(csvfile)

    # extract field names through the first row
    fields = next(csvreader)

    # extract each data row one by one
    for row in csvreader:
        rows.append(row)

    # get total number of rows
    print("total no. of rows: %d"%(csvreader.line_num))

# print the field names
print("Field names are: " + ", ".join(field for field in fields))

# print the first 5 rows of data
print("\nFirst 5 rows are:\n")
for row in rows[:5]:
    #parse each column of a row
    for col in row:
       print("%10s"%col,end=',')
    print("\n")

您需要替换 print("%10s"%col),print("%10s"%col,end=',')


推荐阅读