Google App引擎python以阿拉伯语写作csv [英] Google App engine python writing csv in arabic

查看:169
本文介绍了Google App引擎python以阿拉伯语写作csv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用阿拉伯文写一个CSV文件。我已经将字符串编码为utf-8,并将其写入csv。



问题是,如果我在csv中打开文件,它会显示奇怪的字符,如<$ c $不过,如果我在记事本++中打开文件,它会显示预期的阿拉伯文字。

$ b $



我检查了notepad ++并将编码转换为utf-8而不是utf-8而没有BOM,现在它在csv阅读器(excel)中也能正常工作。所以我应该如何设置编码为utf-8与BOM在应用程序引擎中



我正在使用unicodecsv.writer写入csv

  writer = unicodecsv.writer(self.response.out)
row = []
row.append(transaction.name。 encode('utf8'))
writer.writerow(row)

写入数据存储

解决方案

要使用UTF-8 BOM编写CSV,只需先写BOM;您可以使用 codecs.BOM_UTF8 $ b

 导入编解码器

self.response.out。 write(codecs.BOM_UTF8)
writer = csv.writer(self.response.out)
row = []
row.append(transaction.name.encode('utf8'))
writer.writerow(row)

Excel 2007及更新版本选取BOM并正确打开UTF-8的CSV文件。愚蠢的微软!


I am trying to write a CSV in Arabic script. I have encoded the string to utf-8 and wrote it in the csv.

The problem is if I open the file in csv it shows strange characters like آلز سندويتش كاÙيه however if I open the file in notepad++ it shows the expected arabic text.

i checked notepad ++ and converted the encoding to utf-8 instead of utf-8 without BOM , now its working fine in csv reader(excel) too. so what should i do to set encoding to "utf-8 with BOM" in app engine

i am using unicodecsv.writer to write the csv

writer = unicodecsv.writer(self.response.out)
row = []
row.append(transaction.name.encode('utf8'))
writer.writerow(row)

the data to be written is taken from the datastore

解决方案

To write a CSV with UTF-8 BOM, simply write the BOM first; you can use codecs.BOM_UTF8 for that:

import codecs

self.response.out.write(codecs.BOM_UTF8)
writer = csv.writer(self.response.out)
row = []
row.append(transaction.name.encode('utf8'))
writer.writerow(row)

Excel 2007 and newer pick up on the BOM and correctly open such a CSV file with UTF-8. Silly Microsoft!

这篇关于Google App引擎python以阿拉伯语写作csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆