Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
98 views
in Technique[技术] by (71.8m points)

python - Need to replace ? with space

I'm getting constantly changing text from a website and putting that into an HTML file then having the program read that Html file and find the text and input it into a .txt file but when I input it into the file the spaces are replaced with ?, Some people on discord helped me write one that removes them and they are gone but now they have no spaces and are just one long word. I need help replacing the ?'s with spaces. This is the code I currently have. If anyone is wondering it is, it is supposed to read text from Nitro Type and I know it probably isn't very efficient I don't need it to be I just need to replace the question marks with spaces and .replace() doesn't work with it. Sorry if this is a simple fix I'm super new to coding. FYI this isn't the full code the rest of it works.

with open("word.html", "r") as html_file:
    content = html_file.read()
soup = BeautifulSoup(content, 'lxml')
words = soup.find_all('span', class_='dash-letter')
stuff = ""
for span in words:
    if span.text.isascii():
        stuff += span.text
with open("Sentence.txt", "w") as wf:
    wf.write(stuff)
question from:https://stackoverflow.com/questions/65651530/need-to-replace-with-space

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Not an optimal solution but meets your need

 for span in words:

    if span.text.isascii():
        stuff += span.text
     else:
        stuff += ' '

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...