【发布时间】:2019-11-24 23:00:47
【问题描述】:
我想从字符串中提取一些键及其值。 好像程序需要用到正则表达式,但我不是很了解
好像程序需要用到正则表达式,但我不太了解
字符串是这样的
adprod_type^B6^Apr_flag^B0^Asource^B^Asource_id^B^Aadprod_params^Bheight^D^Chtml^D<style>img{width:100% !important; height:100% !important}</style>\\n<script>gAdController.passback();</script>^Cwidth^D^Aadprodset_id^B9123^Aadserve^B
预期是这样的
adprod_type:'6',pr_flag:'0',source:'',source_id:'',adprod_params:{height:'',html:'<style>img{width:100% !important; height:100% !important}</style>\\n<script>gAdController.passback();</script>',width:''},adprodset_id:'9123',adserve:''
我试过这个:
file_obj = open("/Users/icko/Documents/000/a.txt")
all_lines = file_obj.readlines()
for line in all_lines:
a = line.split('^A')
for i in a:
b = i.split('^B')
key = b[0]
val = b[1]
data[key] = val
if data['adprod_type'] == '6':
print('key=' + key)
print('value=' + val)
file_obj.close()
谢谢
【问题讨论】:
-
到目前为止你尝试过什么?请用代码演示并指出您面临的确切问题。
标签: regex python-3.x string