#help with storing data in to a text file

56 messages · Page 1 of 1 (latest)

trail breachBOT
#

@atomic lynx

prod.exotical Uploaded Some Code

I need help on how i can store the user's order data into a text file. ive created a typical ordering system. i need some new code added to my existing code to store the user's order data into a text file to be retrieved

Uploaded these files to a Gist
terse portal
#

what is your question?

atomic lynx
#

how do i put the user's order into a text file?

#

have u seen my code

#

and where should i put it?

#

@terse portal

terse portal
#

i saw your code

#

where you put it is up to you

#

that's a design decision

#

(if it were me, I would take a command line argument where the user provides the path to a file)

#

as for how to write it, you have to decide how you want to serialize your data. That is, in memory you have a python data structure, but you have to decide on an encoding to convert that structured data into a linear set of bytes to write to disk.

#

probably the simplest and most well documented serialization format would be JSON

#

well, actually, do you need to read this saved file back into memory, or is it just for the user to read on their own in an editor?

#

if you want to read it back into your program later, I would recommend JSON. Otherwise, you could custom format it into a string with the presentation that you find most pleasing to a human eye.

#

either way, once you have the string you want, writing it to a file should be trivial:

def write_string_to_file(path, contents):
  file = open(path, "w") # open with to write
  file.write(contents)
  file.close()
terse portal
#

then i'd pick JSON

atomic lynx
#

is there a new code

terse portal
#

well it's well documented

atomic lynx
#

or i use the same?

terse portal
atomic lynx
#

how would i put this into my code?

#

and where

terse portal
#

did you even read it?

#

is this a school assignment?

atomic lynx
#

yes

terse portal
#

are you just looking for someone to write your code for you?

atomic lynx
#

kinda

terse portal
#

well i'm not going to do that

atomic lynx
#

but i want to learn aswell

#

i just this bit of my code done

#

im have no clue how that all

terse portal
#

if you really want to learn, I think I've given you a lot of good information to start with.

atomic lynx
#

uhhh

#

hold on

#

so if i were to just to store this in a text file normally

#

makibng it it temporary

#

i just use the code above

#

?

#

what im confused about is

#

yk how i the user order is under a variable

#

im wondering how i can code it so that the data (order) under that variable can be saved into a text file

#

@terse portal

terse portal
#

please don't @ me

atomic lynx
#

sorry

#

could you please help

terse portal
#

if you want to learn this is your opportunity. I would recommend the resources I linked above and then experiment.

atomic lynx
#

i dont want to do JSON

#

what was the other option?

regal pelican
atomic lynx
#

i have no clue what to do

#

i'd rather just have the code given and instrcuted where to put it

#

and when i read it i learn that way

vernal frost
#

That is not how you learn. We are here to provide guidance in order for you to learn, not to do the work for you