令人讨厌的编码问题

 jiajiajy323 发布于 2022-12-08 22:42

我检查了这个解决方案,找不到任何有用的问题.我必须使用土耳其语字符,比如"?,?,?,?,ü,Ü,ö,Ö,ç,Ç"当我打开文件并在文件中写入内容时.但这是我的问题:

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中我可以做什么编码?

1 个回答
  • 在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

    2022-12-11 03:09 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有