如何使用python正则表达式从下面的两个字符串中提取数据
TASK000123-Tomcat server hosted on tbu.test1 is down-P1 --In Progress
TASK000123-Tomcat server hosted on tbu.test1 is down-P1 --Completed
我需要以下csv文件:
格式:TaskID,优先级,状态
TASK000123,P1,In Progress
TASK000123,P2,Completed
我怎样才能做到这一点?谢谢你的协助
最佳答案
这是使用简单迭代的一种方法。
例如:
s = """TASK000123-Tomcat server hosted on tbu.test1 is down-P1 --In Progress
TASK000123-Tomcat server hosted on tbu.test1 is down-P1 --Completed"""
result = [["TaskID","Priority","Status"]]
for i in s.splitlines():
val = i.split("-") #Split by '-'
result.append([val[0], val[2], val[-1]])
print(result)
输出:
[['TaskID', 'Priority', 'Status'],
['TASK000123', 'P1 ', 'In Progress'],
['TASK000123', 'P1 ', 'Completed']]
关于python - 正则表达式python数据提取,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53737241/