+ 1
Trying to solve new lines in python core
Here is my code names = ["John", "Oscar", "Jacob"] file = open("names.txt", "w+") file.write('"John" \n"Oscar" \n"Jacob"') file.close() file= open("names.txt", "r") print(file.read()) file.close() It is outputting the names with double colon when it's not meant to
15 Respostas
+ 6
names = ["John", "Oscar", "Jacob"]
file = open("names.txt", "w")
#write down the names into the file
for word in names:
file.write(word+'\n')
file.close()
file= open("names.txt", "r")
print(file.read())
#output the content of file in console
file.close()
+ 1
Line 02: remove single quotation (' ') and use "\n"
+ 1
names = ["John", "Oscar", "Jacob"]
with open("names.txt", "w") as f:
for i in names:
file.write(i+"\n")
with open("names.txt", "r") as f:
print(f.read())
+ 1
names = ["John", "Oscar", "Jacob"]
file = open("names.txt", "w")
#write down the names into the file
for i in range(len(names)):
#Access the names list by i index
file.write(names[i] + "\n")
file.close()
#Print contents of file line by line
file= open("names.txt", "r")
print(file.read())
file.close()
0
Thanks, iTech !
0
names = ["John", "Oscar", "Jacob"]
file = open("names.txt", "w")
#write down the names into the file
for i in range(len(names)):
file.write(names[i])
file.close()
#Print contents of file line by line
file= open("names.txt", "r")
print(file.read())
file.close()
0
Used the join() method to get rid of the brackets and quotation marks.
Not sure if we're supposed to use the readlines() function, I didn't.
Hope this helps.
names = ["John", "Oscar", "Jacob"]
file = open("names.txt", "w+")
names1 = "\n".join(names)
file.write(names1)
file.close()
file.open("names.txt", "r")
print(file.read())
file.close()
0
names = ["John", "Oscar", "Jacob"]
file = open("names.txt", "w+")
file.write('John' '\nOscar' '\nJacob')
file.close()
file= open("names.txt", "r")
print(file.read())
file.close()
This is the correct way to write ur code
this: file.write('"John" \n"Oscar" \n"Jacob"') is close to correct but wrong
its: file.write('John' '\nOscar' '\nJacob')
0
names = ["John", "Oscar", "Jacob"]
file = open("names.txt", "w")
#write down the names into the file
for word in names:
file.write(word+'\n')
file.close()
file= open("names.txt", "r")
print(file.read())
#output the content of file in console
file.close()
0
Shortest way is:
names = ["John", "Oscar", "Jacob"]
file = open("names.txt", "w+")
#write down the names into the file
for word in names:
file.write(word+'\n')
file.close()
file= open("names.txt", "r")
#output the content of file in console
print(file.read())
file.close()
Ask if u have some questions!
0
# The best answer as far as im concerned
names = ["John", "Oscar", "Jacob"]
file = open("names.txt", "w+")
#write down the names into the file
add = file.write(str(names))
file.close()
file = open("names.txt", "r")
#output the content of file in console
for n in names:
print(n)
file.close()
0
# Using while loop
names = ["John", "Oscar", "Jacob"]
with open("names.txt", "w") as file:
i = 0
while i < len(names):
file.write(names[i] + "\n")
i += 1
try:
file = open("names.txt", "r")
print(file.read())
finally: #makes sure file is always closed
file.close()
0
names = ["John", "Oscar", "Jacob"]
file = open("names.txt", "w")
#write down the names into the file
for i in range(len(names)):
file.write(names[i])
file.close()
#Print contents of file line by line
file= open("names.txt", "r")
print(file.read())
0
names = ["John", "Oscar", "Jacob"]
file = open("names.txt", "w+")
for x in names:
file.write(x )
file.write('\n')
#write down the names into the file
file.close()
file= open("names.txt", "r")
print(file.read())
#output the content of file in console
file.close()
0
This works:
names = ["John", "Oscar", "Jacob"]
file = open("names.txt", "w+")
file.write('John\nOscar\nJacob')
file.close()
file= open("names.txt", "r")
print(file.read())
file.close()