Appending an asterisk to the end of each line

Steven D'Aprano steve at pearwood.info
Tue Jul 5 22:35:00 EDT 2016


On Wed, 6 Jul 2016 10:05 am, Seymore4Head wrote:

> On Wed, 6 Jul 2016 01:05:12 +0100, MRAB <python at mrabarnett.plus.com>
> wrote:

>>That suggests to me that it's an encoding problem (the traceback
>>would've indicated that).
>>
>>Specify an encoding when you open the files:
>>
>>f_in = open('win.txt', 'r', encoding='utf-8')
>>f_out = open('win_new.txt', 'w', encoding='utf-8')
>>
>>assuming that 'win.txt' is indeed encoded in UTF-8. (It might be
>>something like ISO-8859-1 instead.)
> 
> Thanks.
> 
> It is working now that I removed those two characters.


And it will break again next time you use the script, if the input file
contains "those characters" (which, I stress, are perfectly reasonable
characters for a text file to contain).

What you should take from this is Chris' advice to learn better debugging
skills: I am absolutely certain that Python will have displayed a traceback
hinting what went wrong, which you could then use to solve the problem for
good rather than just cover it up. If you're not seeing that traceback,
your ability to program is crippled. You'll be like somebody trying to
assemble a jigsaw puzzle in the dark, wearing thick mittens.

So you're first priority ought to be to work out how to run Python code
without the tracebacks disappearing.

(Either that, or make sure your code is perfect, first time, every time.)

At the moment, you're in the situation of a car manufacturer that has just
discovered a defect in their cars:

"If you drive through a puddle with the rear left wheel at high speed, water
splashes up into the body, causes a short in the electronics, and the gas
tank catches fire."

"That's okay, we can just pave over the factory grounds and get rid of the
puddles."

Your script "catches fire" when given input containing non-ASCII characters.
You don't really know why, and you haven't fixed it, but you've "solved"
the problem by "paving over the puddles". Except not really -- the problem
is still there, and if your script ever gets used on a puddle you've
missed, it will catch fire again.




-- 
Steven
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list