Script Generation, help!

D

digital_ant2

I'm new to python, so I'll be at detailed as I can be.

I need to generate a script where each line is like this:

Spam=(x)
Spam=(y)
Spam=(z)

each entry being on their own line, Spam is a string of text and
x,y,and z are random numbers, I also need it to be exported to a text
file with a specified extension.... being completely NEW to python I
push in the right direction would be really helpfull!

Thanks,
Pat
 
F

F. GEIGER

A sketch of a solution could be (untested):

yourText = "%s=(%d)" % (yourString, yourNumber)
try:
f = open(yourFileName, "wt")
f.write(yourText + "\n")
f.close()
except IOError, e:
print "There's a problem writing to file %s: %s" % (yourFileName, e)

Creating strings and numbers and putting that and the write into a loop is
yours ;-)

Kind regards
Franz GEIGER
 
J

John Doe

Some times I think my question are cryptic.

text name = a number. Try a dictionary

myDict = {} # empty defination
myDict[Spam1] = 123939392
myDict[Spam2] = 439322
myDict[Spam3] = 23233.232

Now as to storage with an extension. Thats not so tricky, it's just
that your requirement is so vague.

To save the data myDict ...
import pickle
fp = open('myfile.myspecialext', 'w')
pickle.dump( myDict, fp )
fp.close()

To reload the data ...
import pickle
fp = open( 'myfile.myspecialext', 'r')
myDict = pickle.load( fp )

Now if you have some special format the data is stored in, you may want to
visit Parser. I've not had a chance to use it, but the description is
pretty neat.

Hope this is something like what you want.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,210
Messages
2,571,091
Members
47,692
Latest member
RolandRose

Latest Threads

Top