field_storage.file is a StringIO object. There is no need to open it, because it is already an open file object.
So skip the with and try:
reader = csv.reader(field_storage.file, dialect=csv.excel_tab)
سؤال
I'm trying to read a tab delimited text file uploaded through multipart POST. I'm running this code on Google App Engine using Python/Webapp2/Jinja2.
I get this error in the line indicated below: "AttributeError: (two underscores)exit(two underscores)"
Can you tell me what I'm doing wrong, and how to make this work? Thanks!
class FileReader(webapp2.RequestHandler):
def post(self):
field_storage = self.request.POST.get("file", None)
if isinstance(field_storage.file, cgi.FieldStorage):
with field_storage as csvfile: ## This line causes an error "AttributeError: __exit__"
reader = csv.reader(csvfile, dialect=csv.excel_tab)
for row in reader:
...
المحلول
field_storage.file is a StringIO object. There is no need to open it, because it is already an open file object.
So skip the with and try:
reader = csv.reader(field_storage.file, dialect=csv.excel_tab)