Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9145507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:31:52+00:00 2026-06-17T10:31:52+00:00

In a text file I’m trying to replace, I’m trying to delete an extra

  • 0

In a text file I’m trying to replace, I’m trying to delete an extra line break ie. convert

test="
123"

to

test="123"

What I currently have is:

f = open("file.txt")
o = open("newfile.txt","w")
while 1:
  line = f.readline()
  if not line: break
  line = line.replace('test="\r','test="')
  o.write(line)
o.close()

The problem is that it returns

  test="123"

with an invisible character between the <“> and the <123>. Is there some better alternative to what I’m currently doing? I’m not very proficient with python; thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T10:31:54+00:00Added an answer on June 17, 2026 at 10:31 am

    Your code have some flaws:

    • f is not closed at the end
    • you only replace \r, ignoring the \n on windows – this is your “invisible” character I think (windows uses \r\n as a linebreak in textfiles, linux \n and Mac \r)

    here is another version (might be close to that what you want;) ):

    with file("file.txt", "r") as f:
        with file("newfile.txt", "w") as o:
            o.write("".join(s.replace("\r", "").replace("\n","") 
                    for s in f.readlines()))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text file with lots of links-each line has a link (i.e
I have a text file that has a number in every new line and
The file under test will have text patterns like this: name1: function(){ }, name2:
I have a massive text file of strings ordered by line length descending. I
I have a 384MB text file with 50 million lines. Each line contains 2
I have a huge text file with 25k lines.Inside that text file each line
I have a text file containing a giant list of line numbers which I
I have a text file dump that I need to convert to a delimited
So I have a text file with the following line: 123456789 But then I
I have a text file that I want to edit by rewriting it to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.