Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I removes \n founds between double quotes from a string?

Tags:

python

string

Good day,

I am totally new to Python and I am trying to do something with string.

I would like to remove any \n characters found between double quotes ( " ) only, from a given string :

str = "foo,bar,\n\"hihi\",\"hi\nhi\""

The desired output must be:

foo,bar
"hihi", "hihi"

Edit:

The desired output must be similar to that string: after = "foo,bar,\n\"hihi\",\"hihi\""

Any tips?

like image 700
Cybrix Avatar asked Dec 22 '25 23:12

Cybrix


1 Answers

A simple stateful filter will do the trick.

in_string  = False
input_str  = 'foo,bar,\n"hihi","hi\nhi"'
output_str = ''

for ch in input_str:
    if ch == '"': in_string = not in_string
    if ch == '\n' and in_string: continue
    output_str += ch

print output_str
like image 158
Cat Plus Plus Avatar answered Dec 24 '25 13:12

Cat Plus Plus