第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

從字符串中提取信息并轉(zhuǎn)換為列表

從字符串中提取信息并轉(zhuǎn)換為列表

臨摹微笑 2023-09-05 21:10:46
我有一個(gè)如下所示的字符串:[Base Font : IOHLGA+Trebuchet, Font Size : 3.5324998, Font Weight : 0.0] [(X=250.44,Y=223.48499) height=3.5324998 width=4.2910004]DECEMBER 31,[Base Font : IOFOEO+Imago-Book, Font Size : 3.876, Font Weight : 0.0] [(X=307.5,Y=240.48499) height=3.876 width=2.9970093]respectively. The net decrease in the revenue[Base Font : IOHLGA+Trebuchet, Font Size : 3.5324998, Font Weight : 0.0] [(X=49.5,Y=233.98499) height=3.5324998 width=2.5690002](US$ in millions)我想提取“X”的值和關(guān)聯(lián)的文本并將其轉(zhuǎn)換為列表。請(qǐng)參閱下面的預(yù)期輸出:預(yù)期輸出:['X=250.44','DECEMBER 31,']['X=307.5','respectively. The net decrease in the revenue']['X=49.5','(US$ in millions)']我們?nèi)绾卧?Python 中解決這個(gè)問(wèn)題?我的方法:mylist = []for line in data.split("\n"):    if line.strip():        x_coord = re.findall('^(X=.*)\,$', line)        text = re.findall('^(]\w +)', line)        mylist.append([x_coord, text])我的方法沒(méi)有發(fā)現(xiàn)x_coord和的任何價(jià)值text。
查看完整描述

3 回答

?
郎朗坤

TA貢獻(xiàn)1921條經(jīng)驗(yàn) 獲得超9個(gè)贊

re解決方案:


import re


input = [

    "[Base Font : IOHLGA+Trebuchet, Font Size : 3.5324998, Font Weight : 0.0] [(X=250.44,Y=223.48499) height=3.5324998 width=4.2910004]DECEMBER 31,",

    "[Base Font : IOFOEO+Imago-Book, Font Size : 3.876, Font Weight : 0.0] [(X=307.5,Y=240.48499) height=3.876 width=2.9970093]respectively. The net decrease in the revenue",

    "[Base Font : IOHLGA+Trebuchet, Font Size : 3.5324998, Font Weight : 0.0] [(X=49.5,Y=233.98499) height=3.5324998 width=2.5690002](US$ in millions)",

]


def extract(s):

    match = re.search("(X=\d+(?:\.\d*)?).*?\](.*?)$",s)

    return match.groups()


output = [extract(item) for item in input]

print(output)

輸出:


[

    ('X=250.44', 'DECEMBER 31,'),

    ('X=307.5', 'respectively. The net decrease in the revenue'),

    ('X=49.5', '(US$ in millions)'),

]

解釋:

  • \d... 數(shù)字

  • \d+...一位或多位數(shù)字

  • (?:...)...非捕獲(“正?!保├ㄌ?hào)

  • \.\d*... 點(diǎn)后跟零個(gè)或多個(gè)數(shù)字

  • (?:\.\d*)?...可選(零或一)“小數(shù)部分”

  • (X=\d+(?:\.\d*)?)...第一組,X=number

  • .*?...零個(gè)或多個(gè)任何字符(非貪婪)

  • \]...]符號(hào)

  • $... 字符串結(jié)尾

  • \](.*?)$...第二組,]字符串之間和結(jié)尾之間的任何內(nèi)容


查看完整回答
反對(duì) 回復(fù) 2023-09-05
?
斯蒂芬大帝

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超8個(gè)贊

嘗試這個(gè):


(X=[^,]*)(?:.*])(.*)

import re


source = """[Base Font : IOHLGA+Trebuchet, Font Size : 3.5324998, Font Weight : 0.0] [(X=250.44,Y=223.48499) height=3.5324998 width=4.2910004]DECEMBER 31,

[Base Font : IOFOEO+Imago-Book, Font Size : 3.876, Font Weight : 0.0] [(X=307.5,Y=240.48499) height=3.876 width=2.9970093]respectively. The net decrease in the revenue

[Base Font : IOHLGA+Trebuchet, Font Size : 3.5324998, Font Weight : 0.0] [(X=49.5,Y=233.98499) height=3.5324998 width=2.5690002](US$ in millions)""".split('\n')


pattern = r"(X=[^,]*)(?:.*])(.*)"


for line in source:

    print(re.search(pattern, line).groups())

輸出:


('X=250.44', 'DECEMBER 31,')

('X=307.5', 'respectively. The net decrease in the revenue')

('X=49.5', '(US$ in millions)')

您X=在所有捕獲前面,所以我只做了一個(gè)捕獲組,如果重要的話,請(qǐng)隨意添加非捕獲組。


查看完整回答
反對(duì) 回復(fù) 2023-09-05
?
MYYA

TA貢獻(xiàn)1868條經(jīng)驗(yàn) 獲得超4個(gè)贊

使用帶有命名組的正則表達(dá)式來(lái)捕獲相關(guān)位:


>>> line = "[Base Font : IOHLGA+Trebuchet, Font Size : 3.5324998, Font Weight : 0.0] [(X=250.44,Y=223.48499) height=3.5324998 width=4.2910004]DECEMBER 31,"

>>> m = re.search(r'(?:\(X=)(?P<x_coord>.*?)(?:,.*])(?P<text>.*)$', line)

>>> m.groups()

('250.44', 'DECEMBER 31,')

>>> m['x_coord']

'250.44'

>>> m['text']

'DECEMBER 31,'


查看完整回答
反對(duì) 回復(fù) 2023-09-05
  • 3 回答
  • 0 關(guān)注
  • 219 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)