This issue tracker has been migrated to GitHub ,
and is currently read-only.
For more information,
see the GitHub FAQs in the Python's Developer Guide.
Created on 2011年12月01日 23:42 by mickeyju, last changed 2022年04月11日 14:57 by admin. This issue is now closed.
| Messages (3) | |||
|---|---|---|---|
| msg148747 - (view) | Author: Mickey Ju (mickeyju) | Date: 2011年12月01日 23:42 | |
If this issue has raised previously, then I am sorry for repeating. I did a search but did not find related reports.
Below is the thing I did.
config = configparser.RawConfigParser()
#config.read_file(urlopen(path_config))
config.read_file(open('jkl.ini', 'rb'))
The line commented out was the thing I wanted to do originally. I wanted to parse a configuration file stored on some web server. And I got this error "TypeError: startswith first arg must be bytes or a tuple of bytes, not str." But after I tried, with this line "config.read_file(open('jkl.ini', 'rb'))", the same error can be reproduced. Therefore, I think the error message should be stated another way around as "startswith first arg must be str instead of bytes or a tuple of bytes." I have checked this by adding the lines below to configparser.py after the for-loop at line 994.
print(type(line))
line = str(line, 'utf-8')
print(type(line))
That made the code work.
|
|||
| msg148748 - (view) | Author: Łukasz Langa (lukasz.langa) * (Python committer) | Date: 2011年12月02日 01:04 | |
Hello, Mickey. By doing open('file', 'rb') you're explicitly stating you want the file to be opened in BINARY mode which means it doesn't return strings but bytes. This is not supported anymore in Python 3. This is clearly documented here: http://docs.python.org/dev/library/configparser.html#configparser.ConfigParser.read_file
Same goes for urllib.request.urlopen, it returns bytes because this is all Python knows at the time of reading the URL. If you happen to know the encoding (for instance by parsing the data or headers) you can do something like this: http://docs.python.org/dev/library/urllib.request.html#examples . Your example would become:
config.read_string(urlopen(path_config).read().decode('utf-8'))
Since this is a reasonable mistake to make, I'm leaving this open to adjust the exception raised if read() yields bytes instead of expected strings.
|
|||
| msg161766 - (view) | Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) | Date: 2012年05月28日 10:59 | |
Mickey, you can wrap file-like object returned by urlopen with io.TextIOWrapper. config = configparser.RawConfigParser() config.read_file(io.TextIOWrapper(urlopen(path_config), encoding='utf-8')) Because there is no bug and new feature is not needed, I believe that this issue can be closed. |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022年04月11日 14:57:24 | admin | set | github: 57727 |
| 2012年09月03日 17:26:21 | moijes12 | set | nosy:
- moijes12 |
| 2012年08月25日 09:11:09 | pitrou | set | status: open -> closed resolution: not a bug |
| 2012年08月23日 17:58:51 | moijes12 | set | nosy:
+ moijes12 |
| 2012年05月28日 10:59:28 | serhiy.storchaka | set | nosy:
+ serhiy.storchaka messages: + msg161766 |
| 2011年12月03日 14:56:28 | eric.araujo | set | title: configparser -> configparser can’t read file objects from urlopen |
| 2011年12月02日 01:04:32 | lukasz.langa | set | priority: normal -> low versions: + Python 3.3 messages: + msg148748 assignee: lukasz.langa components: + Library (Lib), - Build keywords: + easy |
| 2011年12月02日 00:45:18 | pitrou | set | nosy:
+ lukasz.langa |
| 2011年12月01日 23:42:55 | mickeyju | create | |