简体   繁体   中英

How to replace character ' " ' randomly repeated in a string with alternating characters ' “ ' and ' ” ' (python)?

I am trying to replace any normal straight quotations (") in a given string with sets of curved open and closed quotations (“ and ”). This would mean the first, third etc "s would be replaced with “, and the second, fourth etc "s would be replaced with ”.

I have tried finding the index of the first quote, creating a splice up to it, and replacing all " in that splice with the “. I have followed that by creating a splice from this new quotes index+1 to the end and replacing all " with ”. The thing is, I am not going to be sure of the length or number of "s in the string provided, and so need to figure out a way to loop some sort of system like this.

This works to only convert a string with 2 quotes properly:

 def convert_quotes(text): '''(str) -> str Convert the straight quotation mark into open/close quotations. >>> convert_quotes('"Hello"') '“Hello”' >>> convert_quotes('"Hi" and "Hello"') '“Hi” and “Hello”' >>> convert_quotes('"') '“' >>> convert_quotes('"""') '“”“' >>> convert_quotes('" "o" "i" "') '“ ”o“ ”i“ ”' ''' find=text.find('"') if find:= -1: for i in text: #first convert first found " to “ text1 = text[.find+1] replace1=text1,replace('"':'“') text2 = text[find+1.] replace2=text2,replace('"','”') text=replace1+replace2 return text

As seen in my docstring, '"Hello" should become “Hello”, but '" "o" "i" "' should become “ ”o“ ”i“ ”.

You might want to collect all locations with quotes, and then change the characters accordingly. This requires an intermediate list of characters ( s_list below):

import re

s = '"Hi" and "Hello"'
s_list = list(s)

quote_position = [p.start() for p in re.finditer('"', s)]

for po, pc in zip(quote_position[::2], quote_position[1::2]):
    s_list[po] = '“'
    s_list[pc] = '”'

s = "".join(s_list)

You can use the re.sub function. I'll use parenthesises for better readability, just replace them with your quotes.

import re

s = """
sdffsd"fsdfsdfdsf fdsf<s" fgdgdfgdf " gfdgdfgd" gdfgdfgdf"
bla re bla
dfsfds " fdsfsdf " fsdfsd "
and the final odd " is here
"""

def func(match): # function for to be called for each sub() step
    return("(" + match.group()[1:-1] + ")")

rex  = re.compile(r'"[^"]*"') # regular expression for a quoted string.

result = rex.sub(func, s) # substitute each match in s with func(match)
result = result.replace('"', '(')  # take care of last remaining " if existing
print(result)

The output would be:

sdffsd(fsdfsdfdsf fdsf<s) fgdgdfgdf ( gfdgdfgd) gdfgdfgdf(
bla re bla
dfsfds ) fdsfsdf ( fsdfsd )
and the final odd ( is here

A second solution without using the re module:

s = """
sdffsd"fsdfsdfdsf fdsf<s" fgdgdfgdf " gfdgdfgd" gdfgdfgdf"
bla re bla
dfsfds " fdsfsdf " fsdfsd "
and the final odd " is here
"""

while True:
    if not '"' in s:
        break
    s = s.replace('"', '(', 1)
    s = s.replace('"', ')', 1)

print(s)

I didn't make any effort to make it efficient. The focus was on being simple.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM