ConfigArgParse icon indicating copy to clipboard operation
ConfigArgParse copied to clipboard

Encoding with utf-8

Open mastrolube opened this issue 3 years ago • 3 comments

Hi there, I have a problem reading a config file. Maybe I'm dumb but I can't find the solution. When opening a config file how can I specify to use utf-8 encoding instead of cp1252? Some people have trouble with not Latin characters and I found out that problem occurs inside configargparse.py/ArgumentParser/parse_known_args.. Maybe I've something to specify in the head of the config file? Any suggestions?

mastrolube avatar Mar 04 '21 14:03 mastrolube

It's currently somewhat cumbersome but you should be able to do it with

def file_open(path, mode="rt"):
     return open(path, mode, encoding='UTF-8')

import configargparse
p = configargparse.ArgumentParser(config_file_open_func=file_open)
...

bw2 avatar Mar 15 '21 02:03 bw2

The default encoding is platform dependent. I wonder if we should explicitly set the encoding to 'UTF-8'. If anyone has a strong opinion on this, let me know.

bw2 avatar Mar 15 '21 14:03 bw2

Hello! Thanks for your answer! Yes, I made something like that a week ago, and works well :)

configargparse.ArgumentParser(
            config_file_open_func=lambda filename: open(
                filename, "r+", encoding="utf-8"
            )

An Arabian windows user of our bot was unable to use config files because of that, my suggestion is to specify utf-8 as the default encoding. I can't see any downside to that.

I also find strange that windows has cp1252 as default .. 😖

mastrolube avatar Mar 15 '21 17:03 mastrolube