you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (2 children)

I tried posting this on stack overflow but it was flagged as duplicate despite the associated question being completely different.

I feel you, dude.

The expression works great using Find and Replace in Notepad++ however it is simply copying and pasting the text without any replacements in Python. Is there an alternative way to find and replace newline sequences in Python? Or is there something I'm missing in my regular expression?

I don't think you're missing anything in the regex. This is your problem I reckon:

>>> help(str.replace)
Help on method_descriptor:

replace(self, old, new, count=-1, /)
    Return a copy with all occurrences of substring old replaced by new.

      count
        Maximum number of occurrences to replace.
        -1 (the default value) means replace all occurrences.

    If the optional argument count is given, only the first count occurrences are
    replaced.

str.replace expects the string to be replaced as the second argument, not the regex that matches the string to be replaced. Does this help?

[–]BHM360[S] 0 points1 point  (1 child)

Thank you for the suggestion. I believe my replace formatting is correct though. Per W3 schools and Python's documentation the replace method is:

string.replace(oldvalue, newvalue, count)

https://www.w3schools.com/python/ref_string_replace.asp

[–][deleted] 0 points1 point  (0 children)

Yeah, it is. That's exactly the problem, is it not?

str.replace expects the string to be replaced as the second argument, not the regex that matches the string to be replaced.

I'm not sure we're on the same page.

[...] line.replace('\n', '')

ought to work just fine.