python 如何把 utf - 8 字符串写入文件中_心若止水_百度空间
2012-03-12 11:34
435 查看
python 如何把 utf - 8 字符串写入文件中_心若止水_百度空间
python 如何把 utf - 8 字符串写入文件中2012-01-11 0:49
from:http://stackoverflow.com/questions/934160/write-to-utf-8-file-in-python 1、open file and write import codecs file=codecs.open("lol","w","utf-8") file.write(u"\u5e10\u53f7\u4e0d\u5b58\u5728")file.close() 2、use pile,but you're careful, and you can see meanother article http://hi.baidu.com/leejun_2005/blog/item/d7d4a40b0c869e0194ca6bac.html 3、关于 python utf-8 的详细介绍请见: How to Use UTF-8 with Python about|archive http://www.evanjones.ca/python-utf8.html 4、Example: dict_temp = {xxx} dict_temp_new = {} for key in dict_temp: value = dict_temp[key] if type(key)==types.UnicodeType: key = key.encode('utf-8') if type(value)==types.UnicodeType: value = value.encode('utf-8') dict_temp_new[key] = value # dict_temp = dict_temp_new _____________________________________________________ import locale import sys import sys, os import types from anyvalue import * from mysql_base import * from debug_utils import * import urllib2 import re import urllib import codecs locale.setlocale(locale.LC_ALL, '') lang, encoding = locale.getdefaultlocale() print 'Locale encoding :', encoding sys.stdout = codecs.getwriter(encoding)(sys.stdout) qq = row[0] email = temp_dict.get("email", 'null') name = temp_dict.get("name", 'null') phone = temp_dict.get("phone", 'null') type_str = temp_dict.get("type", 'null') urlstring = "http://8.8.8.8/qplus/user123?qq=%s&email=%s&name=%s&phone=%s&type=%s" %(qq,email,name.decode('utf-8'),phone,type_str) print urlstring f.write(urlstring.encode("utf-8")+ "\n") url = "http://8.8.8.8/qplus/adduser?" search = [('qq',qq),('email',email),('name',name),('phone',phone),('type',type_str)] getString = url + urllib.urlencode(search) print getString try: r = urllib2.Request(getString) res = urllib2.urlopen(r, None, 10) a = res.readlines() print str(qq) + " : " + str(email) + " : return_code:" + str(eval(a[0])['return_code']) + "" + eval("u"+ "'" + eval(a[0 ])['return_msg'].replace("\\\\", "\\") + "'") except Exception,e: print str(e) print getString |
相关文章推荐
- 如何用idapython脚本获取字符串窗口的数据并写入文件
- 装饰者模式的学习(c#) EF SaveChanges() 报错(转载) C# 四舍五入 保留两位小数(转载) DataGridView样式生成器使用说明 MSSQL如何将查询结果拼接成字符串 快递查询 C# 通过smtp直接发送邮件 C# 带参访问接口,WebClient方式 C# 发送手机短信 文件 日志 写入 与读取
- python中 对文件的读写操作 以及如何边写入 边保存flush()
- 如何用python读取和写入TIFF文件2
- 如何使用Python批量修改文件中有规律的字符串
- UTF-8编码)将字符串写入文件,当字符串中包含中文时,出现写入文件的数据残缺,而当不包含中文时,写入文件的数据正常
- python 在excel文件中写入date日期数据,以及读取excel日期数据,如何在python中正确显示date日期。
- Python 如何将中文变量写入txt文件
- python在处理CSV文件时,字符串和列表写入的区别
- 将字符串按照utf-8或gb2312编码写入文件,10个字节一行,当字符超过10个字节则放到下一行。
- python 如何写入中英文字符串到文件
- python程序1(生成可重复或不可重复随机字符串、生成多个文件夹和文件,并将生成的字符串写入文件中)
- 如何根据CSV格式的第一列作为key,将后几列作为字符串放入map集合并写入文件的操作
- python修改文件中字符串并写入
- linux 在shell下如何 将字符串写入 文件?
- python-12-如何判断字符串a是否以字符串b开头或结尾(改变文件权限)?
- 如何用python读取和写入TIFF文件1
- python(如何将数据写入本地txt文本文件)
- sublime python3中读取和写入文件时如何解决编码问题
- 如何把字符串写入SD卡文件中,如何读取SD卡文件的数据