Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #111161

Re: Appending an asterisk to the end of each line

From MRAB <python@mrabarnett.plus.com>
Newsgroups comp.lang.python
Subject Re: Appending an asterisk to the end of each line
Date 2016-07-06 03:15 +0100
Message-ID <mailman.117.1467771316.2295.python-list@python.org> (permalink)
References (5 earlier) <tehonbhpoe0m2ies2dcnr2502iiq2ecltf@4ax.com> <dab021b9-aa5d-2bc0-d94b-c6d712de59fa@mrabarnett.plus.com> <mailman.116.1467763523.2295.python-list@python.org> <goionbtk49q4dhh6p5fhpfd4ecuhsbpmbe@4ax.com> <497a7e9e-7f5b-62a6-7f2a-af85e3d0dcc0@mrabarnett.plus.com>

Show all headers | View raw


On 2016-07-06 01:05, Seymore4Head wrote:
> On Wed, 6 Jul 2016 01:05:12 +0100, MRAB <python@mrabarnett.plus.com>
> wrote:
>
>>On 2016-07-06 00:45, Seymore4Head wrote:
>>> On Tue, 05 Jul 2016 19:29:21 -0400, Seymore4Head
>>> <Seymore4Head@Hotmail.invalid> wrote:
>>>
>>>>On Tue, 5 Jul 2016 19:15:23 -0400, Joel Goldstick
>>>><joel.goldstick@gmail.com> wrote:
>>>>
>>>>>On Tue, Jul 5, 2016 at 7:03 PM, MRAB <python@mrabarnett.plus.com> wrote:
>>>>>> On 2016-07-05 23:05, Seymore4Head wrote:
>>>>>>>
>>>>>>> import os
>>>>>>>
>>>>>>> f_in = open('win.txt', 'r')
>>>>>>> f_out = open('win_new.txt', 'w')
>>>>>>>
>>>>>>> for line in f_in.read().splitlines():
>>>>>>>     f_out.write(line + " *\n")
>>>>>>>
>>>>>>> f_in.close()
>>>>>>> f_out.close()
>>>>>>>
>>>>>>> os.rename('win.txt', 'win_old.txt')
>>>>>>> os.rename('win_new.txt', 'win.txt')
>>>>>>>
>>>>>>>
>>>>>>> I just tried to reuse this program that was posted several months ago.
>>>>>>> I am using a text flie that is about 200 lines long and have named it
>>>>>>> win.txt.  The file it creates when I run the program is win_new.txt
>>>>>>> but it's empty.
>>>>>>>
>>>>>> Although it creates a file called "win_new.txt", it then renames it to
>>>>>> "win.txt", so "win_new.txt" shouldn't exist.
>>>>>>
>>>>>> Of course, if there's already a file called "win_old.txt", then the first
>>>>>> rename will raise an exception, and you'll have "win_new.txt" and the
>>>>>> original "win.txt".
>>>>>>
>>>>>> --
>>>>>> https://mail.python.org/mailman/listinfo/python-list
>>>>>
>>>>>Why don't you comment out the renames, and see what happens?
>>>>
>>>>I really don't care if the filename gets renamed or not.  I commented
>>>>out the renames, but I still get a new file called win_new.txt and it
>>>>is empty.
>>>>
>>>>The original is unchanged.
>>>
>>> I just tried this on a 3 line text file and it works.
>>>
>>> I am looking through the text file and have found at least two
>>> suspicious characters.  One is a German letter and the other is a
>>> characters that has been replaced by a square symbol.
>>>
>>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.
>
So, you didn't really fix it, you just changed the input to what your 
program is able to handle... :-)

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Appending an asterisk to the end of each line Seymore4Head <Seymore4Head@Hotmail.invalid> - 2016-07-05 18:05 -0400
  Re: Appending an asterisk to the end of each line Joel Goldstick <joel.goldstick@gmail.com> - 2016-07-05 18:27 -0400
    Re: Appending an asterisk to the end of each line Seymore4Head <Seymore4Head@Hotmail.invalid> - 2016-07-05 18:29 -0400
      Re: Appending an asterisk to the end of each line Joel Goldstick <joel.goldstick@gmail.com> - 2016-07-05 18:40 -0400
        Re: Appending an asterisk to the end of each line Seymore4Head <Seymore4Head@Hotmail.invalid> - 2016-07-05 19:04 -0400
          Re: Appending an asterisk to the end of each line Chris Angelico <rosuav@gmail.com> - 2016-07-06 09:38 +1000
            Re: Appending an asterisk to the end of each line Seymore4Head <Seymore4Head@Hotmail.invalid> - 2016-07-05 19:49 -0400
  Re: Appending an asterisk to the end of each line MRAB <python@mrabarnett.plus.com> - 2016-07-06 00:03 +0100
    Re: Appending an asterisk to the end of each line Seymore4Head <Seymore4Head@Hotmail.invalid> - 2016-07-05 19:15 -0400
  Re: Appending an asterisk to the end of each line Joel Goldstick <joel.goldstick@gmail.com> - 2016-07-05 19:15 -0400
    Re: Appending an asterisk to the end of each line Seymore4Head <Seymore4Head@Hotmail.invalid> - 2016-07-05 19:29 -0400
      Re: Appending an asterisk to the end of each line Seymore4Head <Seymore4Head@Hotmail.invalid> - 2016-07-05 19:45 -0400
        Re: Appending an asterisk to the end of each line MRAB <python@mrabarnett.plus.com> - 2016-07-06 01:05 +0100
          Re: Appending an asterisk to the end of each line Seymore4Head <Seymore4Head@Hotmail.invalid> - 2016-07-05 20:05 -0400
            Re: Appending an asterisk to the end of each line MRAB <python@mrabarnett.plus.com> - 2016-07-06 03:15 +0100
            Re: Appending an asterisk to the end of each line Steven D'Aprano <steve@pearwood.info> - 2016-07-06 12:35 +1000
  Re: Appending an asterisk to the end of each line Larry Hudson <orgnut@yahoo.com> - 2016-07-05 21:37 -0700
    Re: Appending an asterisk to the end of each line cs@zip.com.au - 2016-07-06 15:35 +1000

csiph-web