Friday, May 3, 2024
 Popular · Latest · Hot · Upcoming
0
rated 0 times [  0] [ 0]  / answers: 1 / hits: 501  / 2 Years ago, wed, february 2, 2022, 2:26:17

I am writing a program that takes the text written in a text document called datafile.txt, then prints what is in the file. The text that is in the file is is "Hello World", but the text that prints is "['Hello World']" How do i make it so the characters [, ], and ' do not appear.



file = open("datafile.txt", "r")
lines = file.readlines()
SaveDir = lines
print SaveDir

More From » python

 Answers
6

You could try this python code,



import os
file = open("/path/datafile.txt", "r")
lines = file.readlines()
str = ''.join(lines)
print str # => Hello World


.join() function is used to convert a list into string. It prints the file contents as it is without displaying it in list.


[#24667] Wednesday, February 2, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
smelrop

Total Points: 263
Total Questions: 122
Total Answers: 108

Location: Saudi Arabia
Member since Thu, Jan 28, 2021
3 Years ago
;