我想讀取一個csv文件,但由於某種原因,當我要求它打印時,它會打印內存地址而不是表格。閱讀csv文件問題python
下面是我的結果:
>>> read_table('books.csv')
['books.csv', 'boxoffice.csv', 'imdb.csv', 'olympics-locations.csv', 'olympics-results.csv', 'oscar-actor.csv', 'oscar-film.csv', 'seinfeld-episodes.csv', 'seinfeld-foods.csv']
<_csv.reader object at 0x03977C30>
這是我的代碼:
import csv
import glob
from database import *
def read_table(name):
'''
(str) -> Table
Given a file name as a string, the function will return the file as a Table
object.
'''
# create a list that stores all comma-separate files(*.csv)
files_list = glob.glob('*.csv')
print(files_list)
# check if the desired file is in the list
if(name in files_list):
# if found, open the file for reading
with open(name) as csvfile:
readCSV = csv.reader(csvfile, delimiter = ',')
print(readCSV)
東西是在我的腳本錯誤?
根據[documentation](https://docs.python.org/2/library/csv.html),'csv.reader'返回一個reader對象,它將遍歷給定csvfile中的行。所以爲了打印文件,你應該迭代對象,對於每次迭代,對象都會返回一行新的數據。如果需要,可以打印此行。 –