I'm new to programming and trying to supplement my learning by doing some online tutorials. Today, I started looking at working with CSV files using a tutorial that seemed easy enough to follow, but I've ran into what amounts to an immaterial problem, but it's frustrating me that I can't figure it out, haha. I've spent around two hours Googling and testing things, but I'm just not savvy enough to know what to try next. Help, please! haha.
Here's the code in question:
# 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")
The tutorial was actually written for Python 2.X, so the I found the updated formatting for 3.6 and changed that last statement to be:
for col in row:
print('{:>10}'.format(col))
print("\n")
Either way it's written, the results come out in this format:
First 5 rows are:
2013-09-18
66.168571
66.621429
65.808571
66.382858
60.492519
114215500
...
instead of the expected columnar format shown on the tutorial.
I thought I finally found the solution when I read somewhere that you needed the formatting for each item, so I tried:
for col in row:
print('{:>10} {:>10} {:>10} {:>10} {:>10} {:>10} {:>10}'.format(*col))
print("\n")
so that the formatting was there for each column, however that seems to create a column for each letter in the field, e.g:
2 0 1 3 - 0 9
The CSV is just a file of AAPLs stock prices--here's the first 9 rows of data if you want to create a CSV for testing:
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
答案 0 :(得分:2)
# 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")
You need to replace
print("%10s"%col),
with print("%10s"%col,end=',')
答案 1 :(得分:1)
Krishnaa208的答案不太适合我正确的格式。 print("%10s"%col,end=',')
给出了一个包含逗号的表格,每个字段都用引号引起来。但这确实为我指明了正确的方向,即:
# 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('{:>12}'.format(col), end = '')
print("\n")
我的结果是:
First 5 rows are:
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
{:>10}
几乎没有什么联系,因为我的CSV的价格已降至小数点后六位。)
不过,谢谢您的回答。我真的帮了忙!