[英]How do i remove “\n” from an entry within a list?
我已經編輯了原來的問題,因為它不再相關,所以這是新的請求:
我有一個包含以下內容的文本文件:
Sender,Date,Subject
SolarAlert <noreply@example.com>,2019-01-11,SolarAlert Alert: DATAALRT
SolarAlert <noreply@example.com>,2019-01-11,SolarAlert Alert: NETALRT
SolarAlert <noreply@example.com>,2019-01-11,SolarAlert Alert: SFOALRT
使用以下內容,我可以引用我需要的文本(DATAALRT,NETALRT,SFOALRT)。
OPENDOC = open('Alert.txt', 'r')
READDOC = OPENDOC
for line in READDOC:
rows = [line.split(': ', 2)[-1] for line in READDOC]
newrow = rows
print(rows)
print(rows[1])
但是每一行還包含一個“ \\ n”。 我該如何刪除?
非常感謝,
這是讀取'Alert.txt'
並創建一個名為rows
的變量的腳本,其值是['DATAALRT', 'NETALRT', 'SFOALRT']
:
rows = []
with open('Alert.txt', 'r') as READDOC:
for line in READDOC.readlines():
if ': ' in line:
rows.append(line.strip().split(': ', 2)[-1])
print(rows)
但是每一行還包含一個“ \\ n”。 我該如何刪除?
要刪除字符串末尾的“ \\ n”,只需使用str.strip()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.