배경 : Python/AppEngine 프로젝트에서 데이터를 가져 와서 .tsv 파일을 만들어서 d3.js로 차트를 만들 수 있습니다. 지금은 각 페이지로드마다 CSV를 작성하고 있습니다. 대신 파일을 Google Cloud Storage에 한 번 저장하고 거기에서 읽을 수 있습니다. 나는 현재 파일을 쓰고 있어요 어떻게Google Cloud Storage에 저장할 CSV 작성
, 각 시간은 페이지가! :
def get(self): ## this gets called when loading myfile.tsv from d3.js
datalist = MyEntity.all()
self.response.headers['Content-Type'] = 'text/csv'
writer = csv.writer(self.response.out, delimiter='\t')
writer.writerow(['field1', 'field2'])
for eachco in datalist:
writer.writerow([eachco.variable1, eachco.variable2])
를로드 그리고 비효율적 인 반면,이 잘 노력하고 있습니다. this Google Cloud Storage documentation를 사용
는,이 작업 같은 것을 얻기 위해 노력했습니다 :
def get(self):
filename = '/bucket/myfile.tsv'
datalist = MyEntity.all()
bucket_name = os.environ.get('BUCKET_NAME', app_identity.get_default_gcs_bucket_name())
write_retry_params = gcs.RetryParams(backoff_factor=1.1)
writer = csv.writer(self.response.out, delimiter='\t')
gcs_file = gcs.open(filename, 'w', content_type='text/csv', retry_params=write_retry_params)
gcs_file.write(writer.writerow(['field1', 'field2']))
for eachco in datalist:
gcs_file.write(writer.writerow([eachco.variable1, eachco.variable2]))
gcs_file.close()
을하지만 점점 해요 :
TypeError: Expected str but got <type 'NoneType'>.
나는 csv.writer의 출력이 될 것이라고 생각 문자열, 그래서 내가 왜 TypeError지고있어 모르겠어요.
그래서 나는이 상황을 생각할 수 있습니다 : 내가있어- 뭔가 클라우드 스토리지에 TSV를 기록 내 코드에 망쳐. 클라우드 스토리지로 TSV/CSV 파일을 반복하고 쓰는 것이 간단해야합니다.
- 전적으로 완전히 잘못된 방법으로 을 사용 했으므로 BlobStore 또는 db.TextProperty() 을 사용하여이 .tsv 데이터를 저장해야합니다. (파일 크기는 그다지 크지 않으며, 확실히 1MB 미만)
어떤 도움을 주셔서 감사합니다!
편집 - 전체 역 추적
Traceback (most recent call last):
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.1/webapp2.py", line 1530, in __call__
rv = self.router.dispatch(request, response)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.1/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.1/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/mydirectory/myapp/handlers.py", line 21, in dispatch
webapp2.RequestHandler.dispatch(self)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.1/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.1/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/mydirectory/myapp/thisapp.py", line 384, in get
gcs_file.write(writer.writerow(['field1', 'field2']))
File "lib/cloudstorage/storage_api.py", line 754, in write
raise TypeError('Expected str but got %s.' % type(data))
TypeError: Expected str but got <type 'NoneType'>.
응답 헤더를 설정하는 것을 잊지 마십시오. –
전체 추적을 추가 할 수 있습니까? –
이'gcs_file.write (str (writer.writerow ([eachco.variable1, eachco.variable2])))))'줄을 사용하여 달성하고자하는 것이 무엇인지 알 수 없습니다. –