[英]How to split a string and select a specific field
I have a string which I need to split/slice up and set as different variables. 我有一个字符串,我需要拆分/切片并设置为不同的变量。 Here is an example string:
这是一个示例字符串:
Uploaded 09-09 2015, Size 2.05 GiB, ULed by USERX
I need the final output to be something like: 我需要最终的输出是这样的:
date = '09-09 2015' ; size = '2.05' ; user = 'USERX'
Here is the code I am using so far, with the variable 'string' representing the original string: 这是我到目前为止使用的代码,其中变量“ string”代表原始字符串:
date, size, user = string.split(',')
date = date.split()[1], date.split()[2]
size = size.split()[1]
user = user.split()[2]
This works, but I can't help but feel that the code is painfully hacky. 这行得通,但是我忍不住觉得代码很麻烦。 Is there a better way to do this?
有一个更好的方法吗? Thanks so much, may your code by elegant and error free.
非常感谢,希望您的代码优雅而没有错误。
You can use re.findall
: 您可以使用
re.findall
:
import re
s = 'Uploaded 09-09 2015, Size 2.05 GiB, ULed by USERX'
results = re.findall('(?<=Uploaded\s)[\d\-\s]+|(?<=Size\s)[\d\.]+|(?<=ULed by\s)\w+$', s)
d = dict(zip(['date', 'size', 'user'], results))
Output: 输出:
{'date': '09-09 2015', 'size': '2.05', 'user': 'USERX'}
Another one using a mapping
dictionary: 另一个使用
mapping
字典的字典:
import re
string = "Uploaded 09-09 2015, Size 2.05 GiB, ULed by USERX"
mappings = {'Uploaded': 'date', 'Size': 'size', 'by': 'user'}
rx = re.compile(r'(\w+)\s*\b([-. A-Z0-9]{2,})\b')
result = {mappings[m.group(1)]: m.group(2).strip()
for m in rx.finditer(string)}
print(result)
This yields 这产生
{'date': '09-09 2015', 'size': '2.05', 'user': 'USERX'}
See a demo on regex101.com . 参见regex101.com上的演示 。
You might want to try python's named group capturing: 您可能要尝试使用python的命名组捕获:
import re
test_string = 'Uploaded 09-09 2015, Size 2.05 GiB, ULed by USERX'
p = re.compile(r'\w+ (?P<date>\d{2}-\d{2} \d{4}), Size (?P<size>\d+(\.\d+)?) GiB, ULed by (?P<user>\w+)')
result = p.search(test_string)
result.groupdict()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.