object of type _csv.reader has no len(), csv data not recognized

2024/9/30 8:28:52

The following is a self-contained example. Change the "folder_name" to run it. This answers :

reader type = _csv.reader
list(reader) = []
_csv.reader' has no len()

I have tried many things but still cannot access the data, though it is in the file. Any help will be very welcome. Bern

################  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  ################
Answer

mreader is an iterator. It has no len.

But you're consuming it to a list the line before, so you could just do:

mreader = list(mreader)

to convert the iterator into an actual list of rows, which now has a len

Aside: finter.close does nothing. Use finter.close() or a with context block to handle that automatically.

https://en.xdnf.cn/q/70985.html

Related Q&A

Lookup country for GPS coordinates without Internet access

I need to find out in what country given GPS coordinates are, on a device that has no Internet access (e.g. this, but without the easy on-line solution). Having no experience with GIS, I guess Id need …

how to get spyders python recognize external packages on MacOS X?

I have spyderlib installed on my MacOS X (10.6.8) using the official dmg file. In parallel, I have installed packages using both pip and homebrew from the terminal (i.e. opencv, gdal...). As Spyder is …

textcat - architecture extra fields not permitted

Ive been trying to practise what Ive learned from this tutorial:(https://realpython.com/sentiment-analysis-python/) using PyCharm. And this line: textcat.add_label("pos")generated a warning: …

cv2.rectangle() calls overloaded method, although I give other parameter

cv2.rectangle has two ways of calling:img = cv.rectangle( img, pt1, pt2, color[, thickness[, lineType[, shift]]] ) img = cv.rectangle( img, rec, color[, thickness[, lineType[, shift]]]source:h…

Converting xls to csv in Python 3 using xlrd

Im using Python 3.3 with xlrd and csv modules to convert an xls file to csv. This is my code:import xlrd import csvdef csv_from_excel():wb = xlrd.open_workbook(MySpreadsheet.xls)sh = wb.sheet_by_name(S…

HTMLParser.HTMLParser().unescape() doesnt work

I would like to convert HTML entities back to its human readable format, e.g. £ to £, &deg; to etc.Ive read several posts regarding this question Converting html source content into read…

What security issues need to be addressed when working with Google App Engine?

Ive been considering using Google App Engine for a few hobby projects. While they wont be handling any sensitive data, Id still like to make them relatively secure for a number of reasons, like learnin…

Supporting multiple Python module versions (with the same version of Python)

I looked around but cannot find a clear answer to my question.I have a very legitimate need for supporting N-versions of the same Python module.If they are stored in the same same package/directory, th…

ImportError: cannot import name signals

Im using Django 1.3.0 with Python 2.7.1. In every test I write the following imports I get the importError above:from django.utils import unittest from django.test.client import ClientThe full stack tr…

Return a Pandas DataFrame as a data_table from a callback with Plotly Dash for Python

I would like to read a .csv file and return a groupby function as a callback to be displayed as a simple data table with "dash_table" library. @Lawliets helpful answer shows how to do that wi…