Bottom Page

Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Need help with writing to and reading from files?
I am wondering for meaning to split the line by spaces into three values and then print these string objects as they are into the line. Just print the line
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
@wavic The Op asked
(Dec-20-2016, 07:45 PM)birdieman Wrote: 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?
That is why I have shown how to get the values back into the variables.

@birdieman How to use BBCode
The following code:
#Part 2
f = open("test.txt", "w")
your_start_age = 62
her_start_age = 61
start_year = 2017
for y in range(81):
   f.write('{}{}{}\n'.format(start_year, your_start_age, her_start_age))
   your_start_age += 1
   her_start_age += 1
   start_year += 1

#Part 3

with open("test.txt", "r") as opened_file:
   for line in opened_file:
       start_year, your_start_age, her_start_age = line.split()
       print(start_year, your_start_age, her_start_age)
gives me this error for  "...... =.split() " line
ValueError: not enough values to unpack (expected 3, got 1)
sorry for all my ignorance
f.write('{}{}{}\n'.format(start_year, your_start_age, her_start_age))
has not spaces between each {} so there is nothing to split on further down the code
change it to
f.write('{} {} {}\n'.format(startyear, yourstartage, herstartage))
as shown in earlier code.

Edit: Take this in mind that although I have shown how to get each value back into the variables, the values are still strings and not numbers, int or float could be used to convert them back depending on what type of numbers are needed.
works perfect -- thanks to all of you

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Writing list as a file, then reading that file as a list Zoastria_Balnala 3 141 Oct-17-2019, 07:54 PM
Last Post: Zoastria_Balnala
  Reading DBF files from Amazon s3 throwing error - 'int' object has no attribute 'isa abeesm 1 167 Sep-22-2019, 05:49 PM
Last Post: ndc85430
  Reading and writing files JakeHoward4 1 213 Aug-07-2019, 06:22 PM
Last Post: Yoriz
  Problem with reading and writing to file. darktitan 2 222 Jul-20-2019, 06:06 PM
Last Post: darktitan
  Reading PCAP FIles Variables 5 400 Apr-26-2019, 06:05 AM
Last Post: buran
  Python 2.7.13 Issue Reading .txt files Properly username1145 3 357 Mar-24-2019, 03:08 PM
Last Post: username1145
  reading csv and writing csv chawas 2 561 Aug-23-2018, 09:28 AM
Last Post: chawas
  Reading Excel files and creating an output tms21 1 584 Aug-06-2018, 09:02 PM
Last Post: nilamo
  reading 2 files concurrently Skaperen 3 688 Jun-01-2018, 07:07 AM
Last Post: Skaperen
  using subpocess for both reading and writing simultaneously dauriac 11 2,382 May-30-2018, 10:09 PM
Last Post: killerrex

Forum Jump:

Users browsing this thread: 1 Guest(s)