python3和python2的写法不一样具体如下:

python3:

with open(r'd:\ssss.txt','w',encoding='utf-8') as f:

  f.write(u'中文')

python2中open方法是没有encoding这个参数的,如果像python3一样的写法会报异常:TypeError: 'encoding' is an invalid keyword argument for this function

python2中需要加上:

import sys
reload(sys)
sys.setdefaultencoding('utf-8')
with open(r'd:\sss.txt','w') as f:
  f.write(unicode("\xEF\xBB\xBF", "utf-8"))#函数将\xEF\xBB\xBF写到文件开头,指示文件为UTF-8编码。
  f.write(u'中文')
读取文件
with open(r'd:\aaa.txt','r') as ff:
  a= ff.read().encode('gbk')#编码为gbk输出 控制台
  print a
 
或者还有一种写法:
 
import io
with io.open(path,'w',encoding='utf-8') as f:
  f.write(unicode("\xEF\xBB\xBF", "utf-8"))#函数将\xEF\xBB\xBF写到文件开头,指示文件为UTF-8编码。
  f.write(u'这是中文')
 
with open(r'd:\aaa.txt','r') as ff:
  a= unicode(ff.read(),'utf-8')#编码为UTF-8输出
  print a
 

python with open as f 写韩文中文乱码的更多相关文章

  1. nodejs 写服务器解决中文乱码问题

    nodejs 写服务器解决中文乱码问题:https://blog.csdn.net/worldmakewayfordream/article/details/77483423     本文链接:htt ...

  2. python笔记5-python2写csv文件中文乱码问题

    前言 python2最大的坑在于中文编码问题,遇到中文报错首先加u,再各种encode.decode. 当list.tuple.dict里面有中文时,打印出来的是Unicode编码,这个是无解的. 对 ...

  3. python with open as f写中文乱码

    python3和python2的写法不一样具体如下: python3: with open(r'd:\ssss.txt','w',encoding='utf-8') as f: f.write(u'中 ...

  4. Python和Ruby抓取网页时的中文乱码问题(在Eclipse和Apatana Studio下均是这种解决方法

    Python抓取中文网页乱码 :Eclipse+pydev2.2+python2.7  :Apatana Studio3+ pydev2.2+python2.7      run时设置 run--&g ...

  5. 关于java写进mysql中文乱码问题

    Eclipse   windows-preferences-general-workspace选择为UTF-8 Mysql 创建时选择UTF-8 URL = "jdbc:mysql://12 ...

  6. python读取数据库数据,读取出的中文乱码问题

    conn = pymysql.connect( host='127.0.0.1', port=3302, user='username', passwd='password', db=database ...

  7. bufferedwriter写json文件中文乱码

    需要用writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),"utf-8& ...

  8. 【Python】python3 正则爬取网页输出中文乱码解决

    爬取网页时候print输出的时候有中文输出乱码 例如: \\xe4\\xb8\\xad\\xe5\\x8d\\x8e\\xe4\\xb9\\xa6\\xe5\\xb1\\x80 #爬取https:// ...

  9. c#: Noto Sans字体如何支持韩文

    1.源起: VCU10项目,使用了Noto Sans字体,的确漂亮.但验证在win7下,其显示韩文为乱码,颇为头痛. 其界面显示如图: 度娘之,得Noto Sans又有CJK字体,顾名思义,其为支持中 ...

随机推荐

  1. pylab.show()没有显示图形图像(python的matplotlib画图包)

    no display name and no $DISPLAY environment variable ============================ @Neil's answer is ...

  2. EntityFramework 学习 一 Multiple Diagrams in Entity Framework 5.0

    Visual Studio 2012 provides a facility to split the design time visual representation of the Entity ...

  3. mapreduce job提交流程源码级分析(二)(原创)

    上一小节(http://www.cnblogs.com/lxf20061900/p/3643581.html)讲到Job. submit()方法中的: info = jobClient.submitJ ...

  4. 在openstack环境中安装rackspace private cloud --1 环境准备

    在一个openstack环境中安装rackspace private cloud, 环境准备: 在good-net网络中创建3个虚拟机vm Network Detail: good-net Netwo ...

  5. demo(幸福大转盘)总结

    百度推广首页demo 在<head>与</head>之间加入代码 <link rel="shortcut icon" href="favic ...

  6. Rest Web Api Controller 返回JSON格式大小写

    public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.Ro ...

  7. 测试通过!为何线上还有很多BUG?

    质量控制 大多数测试人员认为测试工作是发现bug,虽然这是测试的主要任务,但其实测试最重要的任务是质量控制,而发现bug和验证bug只是质量控制的一个重要环节而已. 我想很多测试人员都经历过这样的场景 ...

  8. poj 1324 状态压缩+bfs

    http://poj.org/problem?id=1324 Holedox Moving Time Limit: 5000MS   Memory Limit: 65536K Total Submis ...

  9. mysql 库,表,数据操作

    一 系统数据库 information_schema: 虚拟库,不占用磁盘空间,存储的是数据库启动后的一些参数,如用户表信息.列信息.权限信息.字符信息等performance_schema: MyS ...

  10. LeetCode OJ:Subsets(子集)

    Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...