Python Forum
[SOLVED] How to replace characters in a string?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] How to replace characters in a string?
#1
Question 
Hello,

I can't get Python to replace a string in a string: If I read the HTML file as binary, it fails; If I open it as text, it fails too :-/

#UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 14167: character maps to <undefined>
#with open(INPUTFILE, 'r') as f:

with open(INPUTFILE, 'rb') as f:
	content_text = f.read()

#TypeError: a bytes-like object is required, not 'str'
#content_text.replace("<i>","[i]")
content_text = str(content_text)
content_text.replace("<i>","[i]")

#for some reason, no matter the input (string or bytes), BS will just output first line
content_text = content_text.encode(encoding='UTF-8')
soup = BeautifulSoup(content_text, 'xml')
#<?xml version="1.0" encoding="utf-8"?>, and then stops
print(soup.prettify())
What's the right way to 1) replace a string in a string, and then 2) have Beautiful Soup parse the input successfully?

Thank you.

---
Edit: Since it's no possible to delete a thread, I'll just add the answer… which was simple enough: Read the file as text telling Python which encoding to use (Windows=Latin1 by default), which BeautifulSoup reads fine (doesn't need to be bytes)

with open(INPUTFILE, 'r',encoding='utf-8') as f:
	content_text = f.read()

content_text.replace("<i>","[i]")

soup = BS(content_text, 'xml')
print(soup.prettify())
Reply
#2
Strings are immutable. The replace() method creates a new string and leaves the original string unchanged.
>>> s = "spam spam <i> spam"
>>> t = s.replace("<i>", "[i]")
>>> t
'spam spam [i] spam'
>>> s
'spam spam <i> spam'
>>> 
Winfried likes this post
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
Good to know, thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] Special characters in XML ForeverNoob 3 1,631 Dec-04-2024, 01:26 PM
Last Post: ForeverNoob
  [SOLVED] Sub string not found in string ? jehoshua 4 1,337 Dec-03-2024, 09:17 PM
Last Post: jehoshua
  Need to replace a string with a file (HTML file) tester_V 1 1,845 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  doing string split with 2 or more split characters Skaperen 22 5,965 Aug-13-2023, 01:57 AM
Last Post: Skaperen
  How do I check if the first X characters of a string are numbers? FirstBornAlbratross 6 3,009 Apr-12-2023, 10:39 AM
Last Post: jefsummers
  Replace string in a nested Dictianory. SpongeB0B 2 2,327 Mar-24-2023, 05:09 PM
Last Post: SpongeB0B
  Replace with upper(string) WJSwan 7 2,696 Feb-10-2023, 10:28 AM
Last Post: WJSwan
  [SOLVED] [BeautifulSoup] Why does it turn inserted string's brackets into &lt;/&gt;? Winfried 0 2,652 Sep-03-2022, 11:21 PM
Last Post: Winfried
  [SOLVED] [BeautifulSoup] Turn select() into comma-separated string? Winfried 0 1,881 Aug-19-2022, 08:07 PM
Last Post: Winfried
  Find and Replace numbers in String giddyhead 2 2,811 Jul-17-2022, 06:22 PM
Last Post: giddyhead

Forum Jump:

User Panel Messages

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