[Tutor] Using files to read data

Alan Gauld alan.gauld at yahoo.co.uk
Tue Jun 27 04:56:39 EDT 2017


On 27/06/17 06:32, Micheal Dale Peterson wrote:
> I am trying to write something that stores x and y data
> with a time reference and then use it later ...

Lets stick to one thing at a time.
How far did you get writing the data to a file?
Did the file get created? Can you edit in the
text editor?

The simplest way to do it is probably

x = 42
y = 66
time = "10:40:22.5"

with open("Myfile.txt") as output:
    output.write(str(x)+'\n')
    output.write(str(y)+'\n')
    output.write(time + '\n')


That only outputs one line to the file Myfile.txt,
but you could put the write lines into a loop if
you had a list of data items.

> the closest i have come is using the json save.  

Using json is a reasonable approach for this type of problem.
Can you show us the actual code you used to write the data?
I assume it did actually get saved to a file in this case?

> Trying to read it back does me no good.

That's too vague to be any use. What does "no good" mean?
Could you open the file from Python?
Did you get any data at all?
Show us some code, even if it doesn't work as you expected.

>  i can print each thing individually. 

What do you mean? Do you mean you can read each
item from the file and print it? If so what is
it that you can't do? Can you save it in a variable?
Can you add the variable to a list for future use?

> everyday i try i either get a bunch of added characters 

Again this this too vague to be helpful.
Show us what kind of characters.
Show us the code you are using to read the data and print it.

> or it separates every character. 

Again show us code, and sample data so we can see.
The problem could lie in how you read the file or
in how you display the data. But without code we can't tell.

> Sorry i can’t  post all of the ways i have tried. 

Just post the latest attempts we can start there.

> I am running OS X version 10.12.5 with python 3.6.1.

Thanks for that, it helps.

> my code to get saved list

There's no code...

Did you send it as a binary attachment maybe?
The list server strips off all binary attachments
Cut 'n paste the code from your text editor into
the mail body.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list