简体   繁体   中英

How to format strings using Python

I have been given a string as shown below

s = 'This is sentence 1."This is sentence 2." This is sentence 3.'

I want the output as below

This is sentence 1.
"This is sentence 2."
This is sentence 3.

I have written following code for this

s = 'This is sentence 1."This is sentence 2." This is sentence 3.'
for i in s.replace('.','.\n').split('\n'):
  print(i.strip())

And below is the output I got

This is sentence 1.
"This is sentence 2.
" This is sentence 3.

The problem is with a double quoted sentence.

I think something can be done with regex, if I can write a regex which can distinguish between. and." then I can solve my problem.

If this is the exact case that you need to solve, the following code will do it:

import re

s = 'This is sentence 1."This is sentence 2." This is sentence 3.'

output = re.findall("This.is.sentence.\d.", s)
output[1] = '"' + output[1] + '"'
for i in range(0, len(output)):
    print(output[i])

Otherwise, a different method would need to be used.

I will try to give you a hint. try adding an IF statement to let your code decide what to do what you want to do exactly. This in case you want to use your code with a larger string.

Solving this for a general case (any number of sentences, of which any number can be quoted) is actualy quite complicated, especially if double quotes can occur not only around, but also within sentences. I think this code works, but I am not completely satisified with it:

import re

s = 'Some sentence. Another sentence. "A quoted sentence." A "sentence" containing quotes. Yet another sentence.'

rx = re.compile(r'"[^"]+?\."\s*|[^"].+?\.\s*')
r = re.match(rx, s)
while r:
   print(r.group(0))
   s = re.sub(rx, '', s, 1)
   r = re.match(rx, s)

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