[英]Trying to print after and before character from a string
在python中,我试图在'©'符号之前和之后提取4个租船者,此代码在©之后提取字符,任何人都可以帮助在©之前打印字符(我不希望整个字符串得到打印,只有少数字符)
import re
html = "This is all test and try things that's going on bro Copyright© Bro Code Bro"
if "©" in html:
symbol=re.findall(r"(?<=©).+$",html,re.M)
print(symbol[0][0:100])
这是一个正则表达式解决方案,用于获取©之前和之后的4个字符
import re
text = "This is all test and try things that's going on bro Copyright© Bro Code Bro"
print(re.findall(".{4}©.{4}", text))
输出:
['ight© Bro']
html = "This is all test and try things that's going on bro Copyright© Bro Code Bro"
html = html.split("©")
print(html[0][-4:])
print(html[1][:4])
输出:
ight
Bro
尝试这样做:
if "©" in html:
pos_c = html.find("©")
symbol = html[pos_c-4:pos_c]
print symbol
你快到了!
使用搜索获取索引,然后根据需要对字符串进行切片/切块
symbol=re.search(r"(?<=©).+$",html).start()
上面的行给出了匹配的索引,在本例中为63
采用
html[symbol:symbol+4] for post and html[symbol-4:symbol] for pre.
请使用python内置函数split()来解决问题。
html = "This is all test and try things that's going on bro Copyright© Bro Code Bro" html = html.split('©')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.