我检查了这个解决方案,找不到任何有用的问题.我必须使用土耳其语字符,比如"?,?,?,?,ü,Ü,ö,Ö,ç,Ç"
当我打开文件并在文件中写入内容时.但这是我的问题:
with open("file1.txt","a","utf-8-sig") as f: f.write(u"????????üüüÜÜ???ii")
我每次都会收到此错误:
with open("file1.txt","a","utf-8-sig") as f: TypeError: an integer is required
真的很烦人.我尝试了一切.
这就是我启动Python脚本的方法:
# -*- coding: cp1254 -*- #!/usr/bin/env python # -*-coding:utf-8-*- from __future__ import division import locale locale.setlocale(locale.LC_ALL, '')
我无法编码文件.如果我试试这个:
with open("file1.txt","a",encoding="utf-8-sig") as f:
我收到了这个错误:
TypeError: 'encoding' is an invalid keyword argument for this function
在Pyhon3x中我可以做什么编码?
在Python 2中,该open()
函数不带encoding
参数.第三个参数是缓冲选项.
您似乎对Python 3版本感到困惑.如果是这样使用io.open()
:
import io with io.open("file1.txt", "a", encoding="utf-8-sig") as f:
在Python 3中,该io.open()
函数替换了Python 2中的版本.
您可能想要研究Unicode和Python:
绝对最低每个软件开发人员绝对必须知道关于Unicode和字符集(没有任何借口!)作者:Joel Spolsky
在Python的Unicode指南
Ned Batchelder的实用Unicode