我正在嘗試將設備的部分輸出寫入 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
提交
取消