Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > fr.comp.lang.python > #3250
| Newsgroups | fr.comp.lang.python |
|---|---|
| Date | 2019-10-25 13:04 -0700 |
| Message-ID | <713a01f1-05ac-4e7c-9153-afc6186ae7be@googlegroups.com> (permalink) |
| Subject | fileinput |
| From | lacsaP Patatetom <patatetom@gmail.com> |
bonjour,
j'ai un petit script python censé ouvrir un fichier journal et afficher son contenu mais comme vous le voyez, une erreur liée à l'encodage survient :
-----------------------
import fileinput
import sys
try:
source = sys.argv[1:]
except IndexError:
source = None
for line in fileinput.input(source):
print(line.strip())
-----------------------
python3.7.4 myscript.py myfile.log
Traceback (most recent call last):
...
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe8 in position 799: invalid continuation byte
python3.7.4 myscript.py < myfile.log
Traceback (most recent call last):
...
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe8 in position 799: invalid continuation byte
-----------------------
j'ajoute le crochet d'encodage pour dépasser l'erreur mais cette fois, le script réagit différemment selon l'entrée utilisée :
-----------------------
import fileinput
import sys
try:
source = sys.argv[1:]
except IndexError:
source = None
for line in fileinput.input(source, openhook=fileinput.hook_encoded("utf-8", "ignore")):
print(line.strip())
-----------------------
python3.7.4 myscript.py myfile.log
first line of myfile.log
...
last line of myfile.log
python3.7.4 myscript.py < myfile.log
Traceback (most recent call last):
...
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe8 in position 799: invalid continuation byte
python3.7.4 myscript.py /dev/stdin < myfile.log
first line of myfile.log
...
last line of myfile.log
python3.7.4 myscript.py - < myfile.log
Traceback (most recent call last):
...
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe8 in position 799: invalid continuation byte
-----------------------
quelqu'un aurait-il une explication et/ou une solution ?
Back to fr.comp.lang.python | Previous | Next — Next in thread | Find similar
fileinput lacsaP Patatetom <patatetom@gmail.com> - 2019-10-25 13:04 -0700
Re: fileinput Jo Engo <yl@icite.fr> - 2019-10-26 07:43 +0000
Re: fileinput lacsaP Patatetom <patatetom@gmail.com> - 2019-10-26 08:15 -0700
Re: fileinput Jo Engo <yl@icite.fr> - 2019-10-27 07:40 +0000
Re: fileinput lacsaP Patatetom <patatetom@gmail.com> - 2019-10-27 01:04 -0700
Re: fileinput lacsaP Patatetom <patatetom@gmail.com> - 2019-10-27 01:08 -0700
Re: fileinput lacsaP Patatetom <patatetom@gmail.com> - 2019-10-27 01:16 -0700
csiph-web