使用 Python 编程,对 "22-10-2022 02:14 : 40150 500 0 from: 'PLC : 0190' " 操作,按照 : 和 from 拆分保留 from,并转化为 DataFrame 格式内容:可以使用正则表达式来对字符串进行拆分,并将结果转换为 DataFrame 格式。下面是一个示例代码:\n\npython\nimport re\nimport pandas as pd\n\n# 输入字符串\nstring = \"22-10-2022 02:14 : 40150 500 0 from: \'PLC : 0190\'\"\n\n# 使用正则表达式对字符串进行拆分\npattern = r\"(?:from: \')(.*)(?:\')\"\nresult = re.findall(pattern, string)\n\n# 将结果转换为 DataFrame 格式\ndf = pd.DataFrame(result, columns=[\'from\'])\n\nprint(df)\n\n\n输出结果为:\n\n\n from\n0 PLC : 0190\n\n\n代码中使用了正则表达式 (?:from: \')(.*)(?:\') 来匹配字符串中以 from: \' 开头,以 \' 结束的部分。然后将匹配到的结果转换为 DataFrame 格式,并指定列名为 \'from\'

使用 Python 正则表达式将字符串拆分并转换为 DataFrame

原文地址: https://www.cveoy.top/t/topic/qflE 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录