我正在尝试将设备的部分输出写入 python 中的文件。这是我的设备输出内容,存储在activity字符串中。927 turu ery7 berk Sun Oct 11 14:26:14.892 2020 UTC how did mark make it 936 fh@df ery0 berk Sun Oct 11 14:26:43.807 2020 UTC make world more peaceful 944 hgk3 ery8 berk Sun Oct 11 14:28:48.385 2020 UTC mpdlf lsdi wewl ok 949 sjff ery9 berk Sun Oct 11 14:31:10.820 2020 UTC no fpp ido sce 我想将UTC之后的所有内容写入文件中。文件内容应该类似于how did mark make it make world more peaceful mpdlf lsdi wewl ok no fpp ido sce
1 回答
www说
TA贡献1775条经验 获得超8个赞
您可以对.split(substring)字符串使用该方法,将字符串转换为按子字符串分割的列表。听起来你想用"UTC"这里的子字符串来分割。分割字符串后,您可以将其写入文件,就像写入常规字符串一样。
例如
myString = "berk Sun Oct 11 14:26:14.892 2020 UTC how did mark make it"
justAfterUTC = myString.split("UTC")[-1] # This is "how did mark make it"
您的最终代码可能如下所示:
with open("output.txt", "a+") as f:
for line in data.split("\n"):
f.write(line.split("UTC")[-1])
添加回答
举报
0/150
提交
取消