python remove characters between quotes
Question:
I tried to remove characters between quotes.
Input string: "a string ,5,to",5,6,my,use,"123456,inside",1,a,b
I would like this output: 5,6,my,use,1,a,b
I tried with re.sub without success:
line = ['"a string ,5,to",5,6,my,use,"123456,inside",1,a,b']
substr = re.sub("["].*?["]", "", line)
many thanks for any help
Answers:
You can match two double-quotes with zero or more non-double-quote characters in between, and substitute the match with an empty string. Also match an optional comma that follows to remove it:
import re
line = '"a string ,5,to",5,6,my,use,"123456,inside",1,a,b'
print(re.sub(r'"[^"]*",?', '', line))
This outputs:
5,6,my,use,1,a,b
maybe try this:
import re
input_string = '"a string ,5,to",5,6,my,use,"123456,inside",1,a,b'
output_list = re.findall(r'"[^"]*?"|w+', input_string)
output_string = ','.join([word for word in output_list if not
word.startswith('"')])
print(output_string)
my output:
5,6,my,use,1,a,b
I tried to remove characters between quotes.
Input string: "a string ,5,to",5,6,my,use,"123456,inside",1,a,b
I would like this output: 5,6,my,use,1,a,b
I tried with re.sub without success:
line = ['"a string ,5,to",5,6,my,use,"123456,inside",1,a,b']
substr = re.sub("["].*?["]", "", line)
many thanks for any help
You can match two double-quotes with zero or more non-double-quote characters in between, and substitute the match with an empty string. Also match an optional comma that follows to remove it:
import re
line = '"a string ,5,to",5,6,my,use,"123456,inside",1,a,b'
print(re.sub(r'"[^"]*",?', '', line))
This outputs:
5,6,my,use,1,a,b
maybe try this:
import re
input_string = '"a string ,5,to",5,6,my,use,"123456,inside",1,a,b'
output_list = re.findall(r'"[^"]*?"|w+', input_string)
output_string = ','.join([word for word in output_list if not
word.startswith('"')])
print(output_string)
my output:
5,6,my,use,1,a,b