使用configparser从utf-8 .ini文件中读取配置时,输入错误

时间:2023-01-05 15:20:36

I'm trying to write a python program using PyCharm and Python 3.3. What I want to do is that my program will copy files from one directory, to one folder or more (depending on the configuration file).

我正在尝试使用PyCharm和python 3.3编写一个python程序。我要做的是,我的程序将文件从一个目录复制到一个或多个文件夹(取决于配置文件)。

Since some of the directories I am trying to copy to the files are in Hebrew, the ini file is utf-8.

由于我试图复制到这些文件的一些目录是希伯来文的,所以ini文件是utf-8。

But, when I read the configuration from that file this is what I get:

但是,当我从这个文件中读取配置时,我得到的是:

C:\Python33\python.exe C:/Users/Username/PycharmProjects/RecorderMover/RecorderMover.py
Traceback (most recent call last):
  File "C:/Users/Username/PycharmProjects/RecorderMover/RecorderMover.py", line 77, in <module>
    sourcePath, destPaths, filesToExclude = readConfig()
  File "C:/Users/Username/PycharmProjects/RecorderMover/RecorderMover.py", line 62, in readConfig
    config = config['RecorderMoverConfiguration']
  File "C:\Python33\lib\configparser.py", line 942, in __getitem__
    raise KeyError(key)
KeyError: 'RecorderMoverConfiguration'

RecorderMover.py:

RecorderMover.py:

def readConfig():
    config = configparser.ConfigParser()

    with codecs.open('RecorderMover.config.ini', 'r', encoding='utf-8') as f:
        config.read(f)

    config = config['RecorderMoverConfiguration']

    sourcePath = config['SourcePath']
    destPaths = config['DestinationPaths']
    filesToExclude = config['FilesToExclude']

RecorderMover.config.ini:

RecorderMover.config.ini:

[RecorderMoverConfiguration]
SourcePath=I:\VOICE\A
DestinationPaths=D:\RoseBackup,E:\רוזה
FilesToExclude=20.08.12.mp3

What am I doing wrong?

我做错了什么?

1 个解决方案

#1


1  

You need to use the .read_file() method on your config instance instead:

您需要在配置实例上使用.read_file()方法:

with open('RecorderMover.config.ini', 'r', encoding='utf-8') as f:
    config.read_file(f)

The .read() method treats f as a sequence of filenames instead, and as none of the lines could ever be interpreted as a filename, the configuration ends up empty.

read()方法将f视为文件名序列,并且由于没有一行可以解释为文件名,因此配置最终为空。

Alternatively, pass in the filename and encoding to .read() without opening the file yourself:

或者,将文件名和编码传递给.read(),而不需要自己打开文件:

config = configparser.ConfigParser()
config.read('RecorderMover.config.ini', encoding='utf-8')

If your input file contains a UTF-8 BOM (\ufeff, a Microsoft devation from the UTF-8 standard) either create the file using a tool that doesn't add that character (e.g. not Notepad), use the utf_8_sig codec to open it:

如果你的输入文件包含一个utf -8 BOM (\ufeff,一个来自utf -8标准的Microsoft devation),或者使用一个不添加那个字符的工具创建文件(例如,不是记事本),使用utf _8_ sig编解码器来打开它:

config = configparser.ConfigParser()
config.read('RecorderMover.config.ini', encoding='utf-8-sig')

#1


1  

You need to use the .read_file() method on your config instance instead:

您需要在配置实例上使用.read_file()方法:

with open('RecorderMover.config.ini', 'r', encoding='utf-8') as f:
    config.read_file(f)

The .read() method treats f as a sequence of filenames instead, and as none of the lines could ever be interpreted as a filename, the configuration ends up empty.

read()方法将f视为文件名序列,并且由于没有一行可以解释为文件名,因此配置最终为空。

Alternatively, pass in the filename and encoding to .read() without opening the file yourself:

或者,将文件名和编码传递给.read(),而不需要自己打开文件:

config = configparser.ConfigParser()
config.read('RecorderMover.config.ini', encoding='utf-8')

If your input file contains a UTF-8 BOM (\ufeff, a Microsoft devation from the UTF-8 standard) either create the file using a tool that doesn't add that character (e.g. not Notepad), use the utf_8_sig codec to open it:

如果你的输入文件包含一个utf -8 BOM (\ufeff,一个来自utf -8标准的Microsoft devation),或者使用一个不添加那个字符的工具创建文件(例如,不是记事本),使用utf _8_ sig编解码器来打开它:

config = configparser.ConfigParser()
config.read('RecorderMover.config.ini', encoding='utf-8-sig')