Python Forum
Need help with writing to and reading from files?
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with writing to and reading from files?
#11
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."
https://freedns.afraid.org
Reply
#12
@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
Reply
#13
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
f.close()

#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
Error:
ValueError: not enough values to unpack (expected 3, got 1)
sorry for all my ignorance
Reply
#14
Your
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.
Reply
#15
works perfect -- thanks to all of you
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Writing into 2 text files from the same function paul18fr 4 1,630 Jul-28-2022, 04:34 AM
Last Post: ndc85430
  Using .hdf5 files only once they are finished writing pyhill00 7 2,722 Nov-25-2021, 06:01 PM
Last Post: pyhill00
  Fastest Way of Writing/Reading Data JamesA 1 2,139 Jul-27-2021, 03:52 PM
Last Post: Larz60+
  Reading Multiple text Files in pyhton Fatim 1 1,886 Jun-25-2021, 01:37 PM
Last Post: deanhystad
  Trouble reading files using pyexcel codebeacon 2 2,140 Feb-08-2021, 05:53 AM
Last Post: codebeacon
  Reading txt files in ssh server. therenaydin 6 4,022 Aug-06-2020, 05:17 AM
Last Post: ndc85430
  How to stop the reading of files Laplace12 4 2,313 Jul-27-2020, 04:07 PM
Last Post: Gribouillis
  How can I speed up my openpyxl program reading Excel .xlsx files? deac33 0 3,363 May-04-2020, 08:02 PM
Last Post: deac33
  Other modules for reading audio files? jedzz 0 1,570 Mar-25-2020, 11:07 PM
Last Post: jedzz
  Error With Reading Files In Directory And Calculating Values chascp 2 2,381 Feb-15-2020, 01:57 PM
Last Post: chascp

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020