簡體   English   中英

使用正則表達式將多行原始字符串轉換為 JSON

[英]Multiline raw string to JSON using regex

我有一個多行原始字符串,想通過在 python 中使用正則表達式來得出一些有意義的見解。

輸入:

raw_input = """this api generates something like this Source: 0
It has been looked into as a random string
event: tomorrow
 Type 0, type: verified
  sector_type: premium sector
  mailing_addr: india
  physical_address_mask: 0x00003fffffffffc0
  serialnum: 3 debit_cards: 1 mod: 0 ranking: 0 devtype: 17 row: 61504 column: 728 
  err_tpe: 2, multiline ECC
  true location: _Nt1_Ch4_Dull0 DOM_K1_"""

在上面的原始字符串中,它遵循相同的格式,以“this api”開頭並以“true location”結尾

預期輸出:

dict = {
‘event’: ‘tomorrow’,
‘sector_type’: ‘premium sector’,
‘mod’: ‘0’,
‘ranking’: ‘0’,
}

我正在努力尋找一種方法來處理這種多行字符串解析,並將其轉換為有意義的見解,例如在 python 中使用正則表達式的 JSON。 有人可以幫助我如何實現這一目標嗎?

我已經編寫了一個特定於您和您的字符串規范共享的多行字符串的正則表達式。

>>> str_val = '''this api generates something like this Source: 0
... It has been looked into as a random string
... event: tomorrow
...  Type 0, type: verified
...   sector_type: premium sector
...   mailing_addr: india
...   physical_address_mask: 0x00003fffffffffc0
...   serialnum: 3 debit_cards: 1 mod: 0 ranking: 0 devtype: 17 row: 61504 column: 728 
...   err_tpe: 2, multiline ECC
...   true location: _Nt1_Ch4_Dull0 DOM_K1_'''
>>> pattern = r'this api generates something like this Source: 0\nIt has been looked into as a random string[\s\S]*(event: (\w*)).*[\s\S]*(sector_type: (.*)).*[\s\S]*(mod: (\w*)).*(ranking: (\w*)).*[\s\S]*true location'
>>> import re
>>> re.findall(pattern, str_val)
[('event: tomorrow', 'tomorrow', 'sector_type: premium sector', 'premium sector', 'mod: 0', '0', 'ranking: 0', '0')]
>>> result = re.findall(pattern, str_val)
>>> result_dict = {'event': result[0][1], 'sector_type': result[0][3], 'mod': result[0][5], 'ranking':  result[0][7]}
>>> result_dict
{'event': 'tomorrow', 'sector_type': 'premium sector', 'mod': '0', 'ranking': '0'}

您可以嘗試使用相關的 Regexr 鏈接: https ://regexr.com/67tft

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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