英文:
Get text before and after colon python
问题
以下是您要翻译的代码部分:
I have following example string:
'EXP DATE: 13.04.2022 PO: P101'
'LOCATION: 111 CONDITION: FN'
I need to split following strings to look like:
{"EXP DATE": "13.04.2022", "PO": "P101"}
{"LOCATION": "111", "CONDITION:" "FN"}
To achieve this i created following function:
def create_key_pair(lst):
return {lst[i].replace(':', ''): lst[i + 1] for i in range(0, len(lst), 2)}
so if i pass
str_ = 'LOCATION: 111 CONDITION: FN'
create_key_pair(str_.split(" "))
i got
{"LOCATION": "111", "CONDITION:" "FN"}
but if i pass
str_ = 'XP DATE: 13.04.2022 PO: P101'
create_key_pair(str_.split(" "))
i got
IndexError: list index out of range
since
EXP DATE
splited by space
英文:
I have following example string:
'EXP DATE: 13.04.2022 PO: P101'
'LOCATION: 111 CONDITION: FN'
I need to split following strings to look like:
{"EXP DATE": "13.04.2022", "PO": "P101"}
{"LOCATION": "111", "CONDITION:" "FN"}
To achieve this i created following function:
def create_key_pair(lst):
return {lst[i].replace(':', ''): lst[i + 1] for i in range(0, len(lst), 2)}
so if i pass
str_ = 'LOCATION: 111 CONDITION: FN'
create_key_pair(str_.split(" "))
i got
{"LOCATION": "111", "CONDITION:" "FN"}
but if i pass
str_ = 'XP DATE: 13.04.2022 PO: P101'
create_key_pair(str_.split(" "))
i got
IndexError: list index out of range
since
EXP DATE
splited by space
答案1
得分: 1
如果值(冒号后面的部分)不能包含空格,以下方法可以工作。该方法使用正则表达式来匹配冒号后的任意字符,然后跟着一个空格,然后是任意数量的非空格字符。然后将每个匹配项按冒号拆分。
import re
def to_dict(s):
matches = re.findall(r".+?: \S+", s)
d = {}
for m in matches:
k, v = m.split(":")
d[k.strip()] = v.strip()
return d
如果值可以包含空格,你将需要找一种分离值和下一个键的方式。JonSG在评论中给出了一个例子"foo: bar bat hat: 10"
。使用上述方法,你会得到{"foo": "bar", "bat hat": "10"}
,但也许你希望得到{"foo": "bar bat", "hat": "10"}
。根据问题中的信息,我只能看到值不包含空格这个模式。
英文:
If the values (the part after the colon) cannot contain spaces, the following will work. The approach uses a regular expression to match any number of characters up to a colon followed by a space and then any number of non-space characters. Then split each match on the colon.
import re
def to_dict(s):
matches = re.findall(r".+?: \S+", s)
d = {}
for m in matches:
k, v = m.split(":")
d[k.strip()] = v.strip()
return d
If the values can contain spaces you'll have to find some other way of separating a value from the next key. JonSG gave the example in the comments of "foo: bar bat hat: 10"
. With the above approach, you'll get {"foo": "bar", "bat hat": "10"}
, but maybe you want {"foo": "bar bat", "hat": "10"}
. From the information in the question the only pattern I can see is that the values do not contain spaces.
答案2
得分: 1
你可以使用正则表达式:
import re
def create_key_pair(s):
data = {}
for kv in re.findall(r'([^:]+:\s*[^\s]+)', s):
k, v = re.split(r':\s*', kv)
data[k.strip()] = v.strip()
return data
用法:
str_ = 'LOCATION: 111 CONDITION: FN'
create_key_pair(str_)
# 输出
{'LOCATION': '111', 'CONDITION': 'FN'}
str_ = 'EXP DATE: 13.04.2022 PO: P101'
create_key_pair(str_)
# 输出
{'EXP DATE': '13.04.2022', 'PO': 'P101'}
英文:
You can use a regex:
import re
def create_key_pair(s):
data = {}
for kv in re.findall('([^:]+:\s*[^\s]+)', s):
k, v = re.split(':\s*', kv)
data[k.strip()] = v.strip()
return data
Usage:
str_ = 'LOCATION: 111 CONDITION: FN'
create_key_pair(str_)
# Output
{'LOCATION': '111', 'CONDITION': 'FN'}
str_ = 'EXP DATE: 13.04.2022 PO: P101'
create_key_pair(str_)
# Output
{'EXP DATE': '13.04.2022', 'PO': 'P101'}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论