簡體   English   中英

嘗試在字符串之前和之后打印

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM