Bottom Page

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
Quote
#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
Quote
#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
Quote
#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.
Quote
#15
works perfect -- thanks to all of you
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Writing the Path to a file properly dcw9996 2 128 Jul-01-2019, 11:26 PM
Last Post: dcw9996
  Random access binary files with mmap - drastically slows with big files danart 1 171 Jun-17-2019, 10:45 AM
Last Post: danart
  Delete Lines that Contain Words - Loop through files in a folder - Write to new files dj99 3 297 May-18-2019, 06:34 AM
Last Post: heiner55
  Avoiding empty line in writing process csv file go127a 4 265 May-10-2019, 01:33 PM
Last Post: go127a
  Writing device driver to stop electric supply to any I/O port sumandas89 0 121 May-02-2019, 10:22 AM
Last Post: sumandas89
  Reading PCAP FIles Variables 5 213 Apr-26-2019, 06:05 AM
Last Post: buran
  csv to xlsx, remove " while writing to excel anna 3 249 Apr-16-2019, 07:57 AM
Last Post: anna
  writing in particular positions clarablanes 5 280 Apr-13-2019, 04:07 PM
Last Post: clarablanes
  Trouble writing over serial EngineerNeil 1 231 Apr-07-2019, 08:17 PM
Last Post: j.crater
  Writing integer to file jpezz 4 403 Apr-02-2019, 07:22 PM
Last Post: jpezz

Forum Jump:


Users browsing this thread: 1 Guest(s)