简体   繁体   English

ConfigParser(Python)中的编码

[英]Encodings in ConfigParser (Python)

Python 3.1.3 What I need is to read dictionary from cp1251-file using ConfigParser. Python 3.1.3我需要的是使用ConfigParser从cp1251-file中读取字典。 My example: 我的例子:

config = configparser.ConfigParser()
config.optionxform = str
config.read("file.cfg")
DataStrings = config.items("DATA")
DataBase = dict()
for Dstr in DataStrings:
    str1 = Dstr[0]
    str2 = Dstr[1]
DataBase[str1] = str2

After that I'm trying to replace some words in some UTF-8 files according dictionary. 之后,我尝试根据字典替换一些UTF-8文件中的某些单词。 But sometimes it doesn't works (for example, with symbols of "new line-carriage return"). 但有时它不起作用(例如,带有“换行符”的符号)。 My file in UTF-8 and configuration file (dictionary) in CP1251. 我的文件位于UTF-8中,而配置文件(词典)位于CP1251中。 Seems like trouble, I have to decode config into UTF-8. 似乎很麻烦,我必须将config解码为UTF-8。 I've tryed this: 我已经尝试过了:

str1 = Dstr[0].encode('cp1251').decode('utf-8-sig')

But error "'utf8' codec can't decode byte 0xcf in position 0" appeared. 但是出现错误"'utf8' codec can't decode byte 0xcf in position 0" If I use .decode('','ignore') - I just lose almost all config file. 如果我使用.decode('','ignore') -我几乎会丢失所有配置文件。 What should I do? 我该怎么办?

Python 3.1 is in the no-mans-land of Python versions. Python 3.1处于Python版本的无人区。 Ideally you'd upgrade to Python 3.5, which would let you do config.read("file.cfg", encoding="cp1251") 理想情况下,您将升级到Python 3.5,这将允许您执行config.read("file.cfg", encoding="cp1251")

If you must stay on 3.1x, you can use the ConfigParser.readfp() method to read from a previously opened file using the correct encoding: 如果必须使用3.1x,则可以使用ConfigParser.readfp()方法以正确的编码从以前打开的文件中读取:

import configparser

config = configparser.ConfigParser()
config.optionxform = str
config_file = open("file.cfg", encoding="cp1251")
config.readfp(config_file)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM