+ 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

6th Apr 2021, 1:36 PM
Simisola Osinowo
Simisola Osinowo - avatar
15 odpowiedzi
+ 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()
7th Aug 2021, 10:16 PM
Yacine
+ 1
Line 02: remove single quotation (' ') and use "\n"
6th Apr 2021, 1:47 PM
Hëllo Wörld🔰
Hëllo Wörld🔰 - avatar
+ 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())
6th Apr 2021, 3:18 PM
iTech
iTech - avatar
+ 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()
27th Jun 2021, 6:35 AM
Mohamed Ikram Ifham
Mohamed Ikram Ifham - avatar
0
Thanks, iTech !
25th Apr 2021, 12:49 PM
Ryan Blewitt
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()
2nd Jun 2021, 4:19 PM
Shubhang
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()
16th Jun 2021, 6:45 PM
Carlos Juarez Fuentes
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')
11th Aug 2021, 1:18 PM
Kevin Orucai
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()
8th Oct 2021, 5:29 PM
Raoof Zakarna
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!
17th Nov 2021, 2:42 PM
Stefan Bartl
Stefan Bartl - avatar
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()
26th Feb 2022, 8:41 PM
Jake Ambrose
Jake Ambrose - avatar
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()
26th Feb 2022, 9:26 PM
Matumbai Binale
Matumbai Binale - avatar
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())
18th May 2022, 1:54 AM
Najwan Najmussabah
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()
13th Dec 2022, 1:34 PM
Noob X Neon
Noob X Neon - avatar
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()
23rd Dec 2023, 8:39 PM
Arun Jayachandran
Arun Jayachandran - avatar