Need help with writing to and reading from files? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Need help with writing to and reading from files? (/thread-1285.html) Pages:
1
2
|
Need help with writing to and reading from files? - birdieman - Dec-20-2016 I am new to this forum and python, as you will discover. I am trying to learn how to write variables to a file that I can later read back. My code is below -- sorry, I do not know how to do 'code tags' (I will learn). I need to know what is wrong with the statement "f.write (startyear, yourstartage, herstartage). I am using PyCharm text editor, and to no really know how to use it either, so please bear with me. yourstartage = 62 herstartage = 61 startyear = 2017 print("Year", "Your", "Her") print("\t ", "Age", "Age") for i in range(81): print(startyear, yourstartage, herstartage) yourstartage += 1 herstartage += 1 startyear += 1 f = open("test.txt", "wb") for y in range(81): f.write(startyear,yourstartage,herstartage) yourstartage += 1 herstartage += 1 startyear += 1 f.close()thanks for looking RE: Newbie question - Larz60+ - Dec-20-2016 try this: f.write('{} {} {}'.format(startyear, yourstartage, herstartage)) RE: Newbie question - birdieman - Dec-20-2016 thank you -- will try it. When I read back the file I just wrote, do I 1) need to open the file, and 2) just change "write" to "read" -- so that I get the values to be associated with the correct variable names in the correct order? thanks again RE: Newbie question - Yoriz - Dec-20-2016 At the moment you have an error from the line f.write(startyear,yourstartage,herstartage)When you have an error you should state the error trace back in your question The error you get is you are passing write 3 arguments when it will only accept one argument that has a str value.Change your write line to f.write('{} {} {}\n'.format(startyear,yourstartage,herstartage))so the format combines all your arguments into one string, the {} are replaced by them and the \n adds a newline to the end. You will also get the error after changing this because you need to change the open flagf = open("test.txt", "wb")to just w f = open("test.txt", "w")The f.close()needs dedenting back to the first coloumn. RE: Newbie question - birdieman - Dec-20-2016 thanks -- RE: Newbie question - Yoriz - Dec-20-2016 When reading back the file the values will be a string on each line, to get back to the variables the formatting that was done will need reversing. In the following code
with open('test.txt', 'r') as opened_file: for line in opened_file: startyear, yourstartage, herstartage = line.strip().split() print(startyear, yourstartage, herstartage) RE: Newbie question - wavic - Dec-20-2016 @Yoriz, Doesn't split() remove '\n'? Isn't '\n' a whitespace?In [3]: import string In [4]: string.whitespace Out[4]: ' \t\n\r\x0b\x0c' In [5]: RE: Need help with writing to and reading from files? - Yoriz - Dec-20-2016 Yea possibly Edit: As pointed out above strip is not necessary in the following line startyear, yourstartage, herstartage = line.strip().split()It can be just startyear, yourstartage, herstartage = line.split() RE: Need help with writing to and reading from files? - birdieman - Dec-20-2016 I made the suggested changes, and get no errors (that I can tell), but the last part of the code does not do what I want it to, which is to read the file I just wrote to, and print out what was read (there should be 80 rows of three variables/values per row). With the code I have, I assumed the print statement would execute each time the file was read, ie, read a row, print it, read a row, print it, etc. (nothing prints out) f = open("test.txt", "w") yourstartage = 62 herstartage = 61 startyear = 2017 for y in range(81): f.write('{}{}{}\n'.format(startyear, yourstartage, herstartage)) yourstartage += 1 herstartage += 1 startyear += 1 with open("test.txt", "r") as opened_file: for line in opened_file: startyear, yourstartage, herstartage = line.split() print(startyear, yourstartage, herstartage)I looked on the forum site to find how to do code tags but could not find reference. sorry. thanks for all of your help - no more questions today RE: Need help with writing to and reading from files? - Larz60+ - Dec-20-2016 You didn't close the file, you need to add f.close() after the first loop. A better way is to use with (which doesn't require a close): with open("test.txt", "w") as f: for y in range(81): ... |