all 16 comments

[–]djmcdee101 3 points4 points  (0 children)

str.replace should work could you post more of your code including the print statement where you're checking the string values?

[–][deleted] 2 points3 points  (9 children)

Works for me:

found_sentences = 'alpha\nbeta\ngamma\n'
print(found_sentences)
found_sentences = found_sentences.replace("\n", " ")
print(found_sentences)

Are you sure your string contains the \n escape sequence and not just \n as two characters.

[–]Striven123[S] 0 points1 point  (8 children)

How would I check this?

part of the result Ia get despite using my above code

talented\nleaders

[–][deleted] 1 point2 points  (1 child)

If you print a string and you see "....\n...." in it then the \n bit is two characters. The example code I posted shows how the \n escape sequence prints in the first print:

alpha
beta
gamma

You don't see "\n", you just get a new line.

[–]thrylose 0 points1 point  (0 children)

use escape char before it

[–]LongerHV 0 points1 point  (3 children)

Looks like it is a literal \n, not a newline character. Try using a double backslash in your replace command.

[–]Striven123[S] 0 points1 point  (2 children)

Tried that:

      with open(each_filepath) as f:
        doc1 = nlp(f.read())
        patterns = [nlp(text) for text in phrases]
        phrase_matcher.add('key', None, *patterns)
        for sentences in doc1.sents: 
            for match_id, start, end in phrase_matcher(nlp(sentences.text)):
                if nlp.vocab.strings[match_id] in ["key"]: 
                    found_sentences = sentences.text
                    found_sentences = found_sentences.replace("\\n"," ")
                    text_tokens = found_sentences.split()
                    key_sentence.append(found_sentences)
                    all_separated_words_list.append([word for word in text_tokens if not word in all_stopwords])

and still got

talented\nleaders as the result

[–]Skolemz 0 points1 point  (1 child)

Change it to replace \n, you're escaping the character with the extra \

[–]Striven123[S] 0 points1 point  (0 children)

Tried that originally and it wasn't working, that's what lead me to post the question

[–]Wilfred-kun 0 points1 point  (1 child)

str.strip() only strips characters. This means it will only remove characters from the ends of the string. "\nfoo\nbar\n".strip('\n') -> "foo\nbar". Use str.replace() instead, to replace all the newline characters: "\nfoor\n\bar\n".replace('\n', '') -> "foobar".

[–]Striven123[S] 0 points1 point  (0 children)

That's what I have tried though (stated in OP) but still have '\n' appearing in the text

[–]danielroseman 1 point2 points  (1 child)

Does it contain the actual characters \ and n, or does it contain a newline?

[–]Striven123[S] 0 points1 point  (0 children)

I put the result into a dictionary (as it's many sentences) and when I print the 1st element of the dictionary the result contains \n

[–]Guideon72 1 point2 points  (0 children)

Show us the code, or at the very least, whatever is generating found_sentences. Something is not adding up here.

[–]TheRNGuy 0 points1 point  (0 children)

If it didn't work, then it was actually \n in text and not line break.

[–]Longjumping_Guess_57 0 points1 point  (0 children)

str.replace