2
以下是一個獨立的示例。更改「folder_name」以運行它。 這個答案:'_csv.reader'類型的對象沒有len(),csv數據未被識別
reader type = _csv.reader
list(reader) = []
_csv.reader' has no len()
我已經嘗試了很多事情,但仍然無法訪問數據,但它是在文件中。任何幫助將是非常受歡迎的。 伯爾尼
################ INPUTS ################
folder_name = '/Users/Shared/TS1/Historiques Dividendes/'
path_to_inter_file = folder_name + 'interm.csv'
################ End INPUTS ################
################ MAIN ################
## --- Creates "inter" file after removing previous one if existed
if os.access(path_to_inter_file, os.W_OK) == True:
os.remove(path_to_inter_file)
if os.access(path_to_inter_file, os.W_OK) == False:
finter = open(path_to_inter_file,'w')
## --- Gets data from an URL
URL = "<http://chart.finance.yahoo.com/table.csv?s=XLB&a=0&b=24&c=1980&d=0&e=24&f=2040&g=v&ignore=.csv'>"
data = requests.get(URL)
## --- Loads data into "inter" file
finter.write(data.text)
finter.close
## --- Reopens "inter" file to read data
finter = open(path_to_inter_file,'r')
mreader = csv.reader(finter,delimiter=',')
print type(mreader)
list(mreader)
print list(mreader)
len(mreader)
## --- Closes "inter" file and removes it
finter.close()
os.remove(path_to_inter_file)
################ End MAIN ################
'finter.close' =>'finter.close()' –