how to save/copy text from text file to html file, without or with formatting to leave the text unchanged.
Example:
> ❯ cat TEST.txt
TEST TEST 1 ONE TEST
234 TEST AGAIN TEST
1234 : : : TEST TEST%
Code:
with open('TEST.txt', 'r') as firstfile, open('RESULT.txt', 'a') as secondfile:
# read content from first file
for line in firstfile:
# append content to second file
secondfile.write(line)
Result:
❯ cat RESULT.txt
TEST TEST 1 ONE TEST
234 TEST AGAIN TEST
1234 : : : TEST TEST%
Now it worked well but when I try to save second file as HTML it has different result where all strings copied in one.
Code:
with open('TEST.txt', 'r') as firstfile, open('RESULT.html', 'a') as secondfile:
# read content from first file
for line in firstfile:
# append content to second file
secondfile.write(line)
Result:
TEST TEST 1 ONE TEST 234 TEST AGAIN TEST 1234 : : : TEST TEST
But I need to copy strings unchanged from txt file to html file.
thank you.
Use html to create new line
with open('TEST.txt', 'r') as firstfile, open('RESULT.html', 'a') as secondfile:
# read content from first file
for line in firstfile:
# append content to second file
secondfile.write(line + '<br/>')
Consider adding newlines.
with open('TEST.txt', 'r') as firstfile, open('RESULT.html', 'a') as secondfile:
# read content from first file
for line in firstfile:
# append content to second file
secondfile.write("\n" + line)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.