• 11

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Remove all line breaks from a long string of text

Basically, I'm asking the user to input a string of text into the console, but the string is very long and includes many line breaks. How would I take the user's string and delete all line breaks to make it a single line of text. My method for acquiring the string is very simple.

string = raw_input("Please enter string: ")

Is there a different way I should be grabbing the string from the user? I'm running Python 2.7.4 on a Mac.

P.S. Clearly I'm a noob, so even if a solution isn't the most efficient, the one that uses the most simple syntax would be appreciated.

      • 2
    • @NicYoung, that is similar but different. strip removes whitespace at the start and end of a string, not inside the string...

How do you enter line breaks with raw_input? But, once you have a string with some characters in it you want to get rid of, just replace them.

>>> mystr = raw_input('please enter string: ')
please enter string: hello world, how do i enter line breaks?
>>> # pressing enter didn't work...
>>> mystr
'hello world, how do i enter line breaks?'
>>> mystr.replace(' ', '')

In the example above, I replaced all spaces. The string '\n' represents newlines. And \r represents carriage returns (if you're on windows, you might be getting these and a second replace will handle them for you!).


# you probably want to use a space ' ' to replace `\n`
mystring = mystring.replace('\n', ' ').replace('\r', '')

Note also, that it is a bad idea to call your variable string, as this shadows the module string. Another name I'd avoid but would love to use sometimes: file. For the same reason.

  • 214
Reply Report
    • This answer is very helpful to me because it mentions the carriage returns. I've tried all methods to remove but still wasn't capturing the characters.
      • 2
    • This usually does the job for me - string.replace(' ', '')). Most log/text-editor files tend to follow this format for new lines.
      • 1
    • You point out not using the variable name string, but for a similar reason you don't want to use the variable name str.
      • 2
    • Worked perfectly, sorry for the stupid question! My initial solution was to list out the string and look for all instances of , but because the list had only 1 character per list entry, the search kept returning false because it would find either or n but not both in the same list entry.

You can split the string with no separator arg, which will treat consecutive whitespace as a single separator (including newlines and tabs). Then join using a space:

In : " ".join("\n\nsome    text \r\n with multiple whitespace".split())
Out: 'some text with multiple whitespace'


  • 25
Reply Report

updated based on Xbello comment:

string = my_string.rstrip('\r\n')

read more here

  • 15
Reply Report
      • 1
    • I've been just bitten by this. If you specify the to rstrip, fails. If you don't specify nothing, spaces, and possibly others are trimmed. You have to use rstrip(" ")
      • 1
    • had a string where just rstrip(' ') was not enough and had to go with: my_string.rstrip(' ').replace(' ', ' ')

A method taking into consideration

  • additional white characters at the beginning/end of string
  • additional white characters at the beginning/end of every line
  • various end-line characters

it takes such a multi-line string which may be messy e.g.

test_str = '\nhej ho \n aaa\r\n   a\n '

and produces nice one-line string

>>> ' '.join([line.strip() for line in test_str.strip().splitlines()])
'hej ho aaa a'

UPDATE: To fix multiple new-line character producing redundant spaces:

' '.join([line.strip() for line in test_str.strip().splitlines() if line.strip()])

This works for the following too test_str = '\nhej ho \n aaa\r\n\n\n\n\n a\n '

  • 3
Reply Report
      • 1
    • This doesn't handle the case of contiguous line feeds in the middle of the string. Two line feeds result in two contiguous blanks in the output. Try "test_str = ' hej ho aaa a '"

If anybody decides to use replace, you should try r'\n' instead '\n'

mystring = mystring.replace(r'\n', ' ').replace(r'\r', '')
  • 2
Reply Report
      • 1
    • In my case, I needed to do this: 1. Get HTML code from DB 2. Get needed text from HTML 3. Remove all newline from text 4. Insert edited text to a spreadsheet document And it didn't work properly, unless I used r ( "raw string literal"). Unfortunately, I have no idea why )

The problem with rstrip is that it does not work in all cases (as I myself have seen few). Instead you can use - text= text.replace("\n"," ") this will remove all new line \n with a space.

Thanks in advance guys for your upvotes.

  • 1
Reply Report

Trending Tags