<tfoot id='zRMKM'></tfoot>

      <small id='zRMKM'></small><noframes id='zRMKM'>

        <bdo id='zRMKM'></bdo><ul id='zRMKM'></ul>
      <i id='zRMKM'><tr id='zRMKM'><dt id='zRMKM'><q id='zRMKM'><span id='zRMKM'><b id='zRMKM'><form id='zRMKM'><ins id='zRMKM'></ins><ul id='zRMKM'></ul><sub id='zRMKM'></sub></form><legend id='zRMKM'></legend><bdo id='zRMKM'><pre id='zRMKM'><center id='zRMKM'></center></pre></bdo></b><th id='zRMKM'></th></span></q></dt></tr></i><div id='zRMKM'><tfoot id='zRMKM'></tfoot><dl id='zRMKM'><fieldset id='zRMKM'></fieldset></dl></div>
        <legend id='zRMKM'><style id='zRMKM'><dir id='zRMKM'><q id='zRMKM'></q></dir></style></legend>
      1. 写入 CSV 以存储在 Google Cloud Storage 中

        时间:2023-11-07
        <i id='fvz2M'><tr id='fvz2M'><dt id='fvz2M'><q id='fvz2M'><span id='fvz2M'><b id='fvz2M'><form id='fvz2M'><ins id='fvz2M'></ins><ul id='fvz2M'></ul><sub id='fvz2M'></sub></form><legend id='fvz2M'></legend><bdo id='fvz2M'><pre id='fvz2M'><center id='fvz2M'></center></pre></bdo></b><th id='fvz2M'></th></span></q></dt></tr></i><div id='fvz2M'><tfoot id='fvz2M'></tfoot><dl id='fvz2M'><fieldset id='fvz2M'></fieldset></dl></div>
          <tbody id='fvz2M'></tbody>

          <small id='fvz2M'></small><noframes id='fvz2M'>

                <bdo id='fvz2M'></bdo><ul id='fvz2M'></ul>
              • <legend id='fvz2M'><style id='fvz2M'><dir id='fvz2M'><q id='fvz2M'></q></dir></style></legend>

                1. <tfoot id='fvz2M'></tfoot>

                2. 本文介绍了写入 CSV 以存储在 Google Cloud Storage 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  Background: I'm taking data in my Python/AppEngine project and creating a .tsv file so that I can create charts with d3.js. Right now I'm writing the CSV with each page load; I want to instead store the file once in Google Cloud Storage and read it from there.

                  How I'm currently writing the file, each time the page is loaded!:

                  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='	')
                      writer.writerow(['field1', 'field2'])
                      for eachco in datalist:
                          writer.writerow([eachco.variable1, eachco.variable2])
                  

                  And while inefficient, this is working just fine.

                  Using this Google Cloud Storage documentation, I've been trying to get something like this working:

                  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='	')
                      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()
                  

                  But I'm getting:

                  TypeError: Expected str but got <type 'NoneType'>.
                  

                  I thought that the output of csv.writer would be a string, so I'm not sure why I'm getting the TypeError.

                  So I can think of two situations:

                  1. I've got something screwed up in my code that writes the tsv to Cloud Storage. It should be simple to iterate through and write a TSV/CSV file to Cloud Storage though, right?
                  2. I've gone about this the completely wrong way entirely, and should maybe even use BlobStore or db.TextProperty() to store this .tsv data. (The files aren't that big; definitely well under 1MB)

                  I'd appreciate any help!

                  edit - full traceback

                  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'>.
                  

                  解决方案

                  You're still attempting to create the writer on a response:

                  writer = csv.writer(self.response.out, delimiter='	')
                  

                  You need to write to the GCS file. Something like this:

                      datalist = MyEntity.all()
                      bucket_name = os.environ.get('BUCKET_NAME', app_identity.get_default_gcs_bucket_name())
                      filename = os.path.join(bucket_name, 'myfile.tsv')
                      write_retry_params = gcs.RetryParams(backoff_factor=1.1)
                      gcs_file = gcs.open(filename, 'w', content_type='text/csv', retry_params=write_retry_params)
                      writer = csv.writer(gcs_file, delimiter='	')
                      writer.writerow(['field1', 'field2'])
                      for eachco in datalist:
                          writer.writerow([eachco.variable1, eachco.variable2])
                      gcs_file.close()
                  

                  Notes:

                  • not actually tested
                  • I also adjusted the filename to use bucket_name
                  • if you do this in the get() request you may want to check if the file already exists and, if so, use it, otherwise you'd be still generating it at every request. Alternatively you could move this code on a task or in the .tsv upload handler.

                  这篇关于写入 CSV 以存储在 Google Cloud Storage 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 gcloud-python 在 Google Cloud Storage 中设置元数据 下一篇:如何检查文件是否存在于 Google Cloud Storage 中?

                  相关文章

                  <legend id='aI1Nv'><style id='aI1Nv'><dir id='aI1Nv'><q id='aI1Nv'></q></dir></style></legend>

                  1. <tfoot id='aI1Nv'></tfoot>
                    <i id='aI1Nv'><tr id='aI1Nv'><dt id='aI1Nv'><q id='aI1Nv'><span id='aI1Nv'><b id='aI1Nv'><form id='aI1Nv'><ins id='aI1Nv'></ins><ul id='aI1Nv'></ul><sub id='aI1Nv'></sub></form><legend id='aI1Nv'></legend><bdo id='aI1Nv'><pre id='aI1Nv'><center id='aI1Nv'></center></pre></bdo></b><th id='aI1Nv'></th></span></q></dt></tr></i><div id='aI1Nv'><tfoot id='aI1Nv'></tfoot><dl id='aI1Nv'><fieldset id='aI1Nv'></fieldset></dl></div>
                      <bdo id='aI1Nv'></bdo><ul id='aI1Nv'></ul>

                    1. <small id='aI1Nv'></small><noframes id='aI1Nv'>