Problem with Python script

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
SkullyRipz
Posts: 24
Joined: Fri 02 Mar 2012, 16:50

Problem with Python script

#1 Post by SkullyRipz »

Hi all. I'm currently teaching myself python and am stuck on a particular script I am trying to create. I am trying to create a list of times and run a function to sort them from low to high. I am then trying to take that output and print the results as text into a new file. I know I have the code to sort the data right because it outputs it onto the screen correctly when I run that portion of the script. The "write" command to output that data to the new file is telling me I can only write literal strings. Is there any way around this or is it possible? Or any other command I should be using instead? Am I anywhere near headed in the right direction?

Here's what I have so far:

Code: Select all

from sys import argv

script, NewFile = argv

timeList = []

resultsFile = open ( 'raceRlist' )

for resultsLine in resultsFile:
	fieldList = resultsLine.split(None, 1)
	
	raceTuple = (float(fieldList[0]), fieldList[1].rstrip())
	timeList.append ( raceTuple )
	
timeList.sort()

Header = "Finish  Time  Name"
Underline = "------  ----  ----"
for position in range(len(timeList)):
	raceTuple = timeList[position]
	print "%4d  %6.1f %2s"% (position+1, raceTuple[0], raceTuple[1])
	
Create = open (NewFile, 'w')
Create.write(Header)
Create.write("\n")
Create.write(Underline)
Create.write("\n")
Create.write() #--------This is where I'm stuck.  How do I write the complete
Create.write("\n")  #---sorted list to the new file?

print "If all went according to plan, there should be a new file with the\n"
print "results printed in the correct format."

resultsFile.close()
Create.close()

User avatar
antiloquax
Posts: 405
Joined: Fri 27 Jan 2012, 09:17

#2 Post by antiloquax »

I think it might be:

Code: Select all

Create.writelines()
My System:Arch-Arm on RPi!
"[url=http://murga-linux.com/puppy/viewtopic.php?t=76049l]RacyPy[/url]" puplet on Toshiba Tecra 8200. PIII, 256 MB RAM.
[url=http://raspberrypy.tumblr.com/]RaspberryPy[/url]: Lobster and I blog about the RPi.

Post Reply