以UTF-8格式导出为CSV格式

时间:2014-11-05 17:58:52

标签: python google-app-engine csv utf-8

我有一个CSV文件,该文件位于" UTF-16LE"格式。该文件是德语,因此有很多德语口音。我使用Blobstore将其导入到GAE中,使用Python读取器对象创建列表,操作列表并将另一个列表输出到CSV。当我创建列表并运行' chardet'它显示为ASCII格式。此外,在输出文件时,重音字符消失。

作为Python的相对新手,我还在学习并阅读了很多有关编码和解码的知识。但是,在这个问题上花了很长时间后,我仍然无法找到解决方案。非常感谢任何帮助!

以下代码:

进口:

class MainHandler(webapp2.RequestHandler):
  def get(self):

    upload_url = blobstore.create_upload_url('/upload')
    self.response.out.write('<html><body>')
    self.response.out.write('<form action="%s" method="POST" enctype="multipart/form-data">' % upload_url)
    self.response.out.write("""Entity: <input type="text" name="entity"><br>Upload File: <input type="file" name="file1"><br><input type="submit"
        name="submit" value="Submit"> </form></body></html>""")

class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
  def post(self):
    upload_files = self.get_uploads('file1')  # 'file' is file upload field in the form
    blob_info = upload_files[0]
    blob_reader = blobstore.BlobReader(blob_info.key())
    blob_iterator = BlobIterator(blob_reader)
    file = csv.reader((x.replace('\0', '') for x in blob_iterator),skipinitialspace=True, delimiter='\t')

    for row in file:
        if row:
            file2.append(row)

--
AND CODE TO EXPORT:

   self.response.headers['Content-Type'] = 'application/csv'
    self.response.headers['Content-Disposition'] = 'attachment; filename=output.csv'
    wr = csv.writer(self.response.out, quoting=csv.QUOTE_ALL)

    for row in finallist:
         wr.writerow(row) 

0 个答案:

没有答案