Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Regarding file editing
#6
You are confused. Understandably so.

The problem is as I said. Powershell is putting a bunch of extra stuff in your from_file. If you made your file using notepad or even windows command shell the extra stuff does not get put in the from_file, and you program will produce the expected results. You could also modify your program to understand the extra stuff that powershell adds to the file.

So what is the extra stuff? It is multi-byte character encoding. When powershell writes your text to a file, it writes it as unicode characters. The default encoding (from what I can see) is utf-16. When you open the file in Python without specifying any encoding it assumes the file encoding is utf8. Even though your test string appears to be 20 characters long, when generated using powershell it is actually 44 bytes long. (20 printable characters + carriage return + linefeed) * 2. What your program sees is 20 visible characters, 20 empty characters (0x00) and a confusing mess at the end where it tries to replace carriage and linefeed characters with a single linefeed when reading the file, and converting that back to a carriage and linefeed when writing.

One way to fix this is specify the file encoding when you open the from_file.
indata =open(from_file, encoding="utf-16").read()
Of course now it will mess up reading files that use utf-8 encoding.

Your choice. Modify the file encoding to match your program or change your program encoding to match the file.

I bet your book was written for python 2.
Reply


Messages In This Thread
Regarding file editing - by dan86 - Jun-09-2022, 03:22 PM
RE: Regarding file editing - by deanhystad - Jun-09-2022, 04:43 PM
RE: Regarding file editing - by dan86 - Jun-09-2022, 04:49 PM
RE: Regarding file editing - by deanhystad - Jun-09-2022, 05:01 PM
RE: Regarding file editing - by dan86 - Jun-09-2022, 05:06 PM
RE: Regarding file editing - by deanhystad - Jun-09-2022, 08:23 PM
RE: Regarding file editing - by dan86 - Jun-10-2022, 01:11 PM
RE: Regarding file editing - by deanhystad - Jun-10-2022, 01:24 PM
RE: Regarding file editing - by dan86 - Jun-29-2022, 10:25 AM
RE: Regarding file editing - by Larz60+ - Jun-29-2022, 11:01 AM
RE: Regarding file editing - by dan86 - Jun-29-2022, 11:45 AM
RE: Regarding file editing - by snippsat - Jun-29-2022, 12:26 PM
RE: Regarding file editing - by dan86 - Jun-29-2022, 07:13 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  xml file editing with lxml.etree FlavioBueno 2 784 Jun-09-2023, 02:00 PM
Last Post: FlavioBueno
  Need help editing a PDF omar 4 1,169 Oct-22-2022, 08:52 PM
Last Post: Larz60+
  pdf file processing: how to "Enable Editing" Pavel_47 4 3,310 Dec-04-2019, 10:00 AM
Last Post: Pavel_47
  Parsing and Editing a Structured Text File norsemanGrey 1 2,488 Jul-11-2018, 09:51 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020