在 Python 中读取文本文件时如何评估转义字符
How to evaluate escape character when reading a text file in Python
我有这样的文本文件:
["RUN DATE: 2/08/18 9:00:24 USER:XXXXXX DISPLAY: MENULIST PROG NAME: MH4567 PAGE 1\nMENU: ADCS00 Visual Basic Things
Service\n 80 Printer / Message Control\n 90 Sign Off\nSelection or
command\n===>____________________________________________________________________________\n____________________________________________________________________________
____\n F3=Exit F4=Prompt F9=Retrieve F12=Previous\n 80 CALL PGM(GUCMD)\nAUTHORIZED: DOAPROCESS FDOAPROCES FOESUPR FPROGRAMMR OESUPR PROGRAMMER\n
90 SIGNOFF\nAUTHORIZED: DOAPROCESS FDOAPROCES FOESUPR FPROGRAMMR OESUPR PROGRAMMER\n", "RUN DATE: 5/09/19 9:00:24 USER:XXXXXX DISPLAY:
MENULIST PROG NAME: MH4567 PAGE 2\nMENU: APM001 Accounts Payable Menu\n MENU
OPTIONS DISPLAY PROGRAMS\n 1 Radar Processing 30 Vendor\n 2 Prepaid Processing
31 Prepaid\n"]
I want to translate every '\n' as new line and then print.
我试过这段代码,但没有成功:
import json
contents = open("file.txt", "r")
Lines = contents.readlines()
for l in Lines:
print(l)
上面的代码将 .txt 文件中的行尾打印为新行而不观察实际的 '\n' 字符?
使用-
with open('./sample.txt', 'r') as file_:
txt = file_.read().replace('\n', '\n')
print(txt)
输出
"RUN DATE: 2/08/18 9:00:24 USER:XXXXXX DISPLAY: MENULIST PROG NAME: MH4567 PAGE 1
MENU: ADCS00 Visual Basic Things
Service
80 Printer / Message Control
90 Sign Off
Selection or
command
===>____________________________________________________________________________
____________________________________________________________________________
____
F3=Exit F4=Prompt F9=Retrieve F12=Previous
80 CALL PGM(GUCMD)
AUTHORIZED: DOAPROCESS FDOAPROCES FOESUPR FPROGRAMMR OESUPR PROGRAMMER
90 SIGNOFF
AUTHORIZED: DOAPROCESS FDOAPROCES FOESUPR FPROGRAMMR OESUPR PROGRAMMER
", "RUN DATE: 5/09/19 9:00:24 USER:XXXXXX DISPLAY:
MENULIST PROG NAME: MH4567 PAGE 2
MENU: APM001 Accounts Payable Menu
MENU
OPTIONS DISPLAY PROGRAMS
1 Radar Processing 30 Vendor
2 Prepaid Processing
31 Prepaid
"
我有这样的文本文件:
["RUN DATE: 2/08/18 9:00:24 USER:XXXXXX DISPLAY: MENULIST PROG NAME: MH4567 PAGE 1\nMENU: ADCS00 Visual Basic Things
Service\n 80 Printer / Message Control\n 90 Sign Off\nSelection or
command\n===>____________________________________________________________________________\n____________________________________________________________________________
____\n F3=Exit F4=Prompt F9=Retrieve F12=Previous\n 80 CALL PGM(GUCMD)\nAUTHORIZED: DOAPROCESS FDOAPROCES FOESUPR FPROGRAMMR OESUPR PROGRAMMER\n
90 SIGNOFF\nAUTHORIZED: DOAPROCESS FDOAPROCES FOESUPR FPROGRAMMR OESUPR PROGRAMMER\n", "RUN DATE: 5/09/19 9:00:24 USER:XXXXXX DISPLAY:
MENULIST PROG NAME: MH4567 PAGE 2\nMENU: APM001 Accounts Payable Menu\n MENU
OPTIONS DISPLAY PROGRAMS\n 1 Radar Processing 30 Vendor\n 2 Prepaid Processing
31 Prepaid\n"]
I want to translate every '\n' as new line and then print.
我试过这段代码,但没有成功:
import json
contents = open("file.txt", "r")
Lines = contents.readlines()
for l in Lines:
print(l)
上面的代码将 .txt 文件中的行尾打印为新行而不观察实际的 '\n' 字符?
使用-
with open('./sample.txt', 'r') as file_:
txt = file_.read().replace('\n', '\n')
print(txt)
输出
"RUN DATE: 2/08/18 9:00:24 USER:XXXXXX DISPLAY: MENULIST PROG NAME: MH4567 PAGE 1
MENU: ADCS00 Visual Basic Things
Service
80 Printer / Message Control
90 Sign Off
Selection or
command
===>____________________________________________________________________________
____________________________________________________________________________
____
F3=Exit F4=Prompt F9=Retrieve F12=Previous
80 CALL PGM(GUCMD)
AUTHORIZED: DOAPROCESS FDOAPROCES FOESUPR FPROGRAMMR OESUPR PROGRAMMER
90 SIGNOFF
AUTHORIZED: DOAPROCESS FDOAPROCES FOESUPR FPROGRAMMR OESUPR PROGRAMMER
", "RUN DATE: 5/09/19 9:00:24 USER:XXXXXX DISPLAY:
MENULIST PROG NAME: MH4567 PAGE 2
MENU: APM001 Accounts Payable Menu
MENU
OPTIONS DISPLAY PROGRAMS
1 Radar Processing 30 Vendor
2 Prepaid Processing
31 Prepaid
"