Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Ghostdog gave me this script and it works perfect other that I dont know how to save it to a file it just prints on the screen. Any help would be appreciated. I am trying to learn python but I need some help to get this working sooner than my learning curve will allow lol. Here is the program.
for lines in open("test.ext"):
lines=lines.strip() #strip newline
x,y = lines.split(":") # now x contains xxxxxx, and y contains yyyyyy-zzzzz
y = y.replace("-",";") # replace all - to ;
print y,

I have replied to your previous post. If you want to write to file,
c:\test> python script.py > outputfilethis is provided you only want 1 output file. If you need to write to many files, do it inside Python.
Read the docs.

Ghostdog maybe you could give me just a little more help? I have my program almost perfected. Well I should say your program. Still not sure why it dont compile my whole list. Original file is about 6.5mb and my output is only 1.5mb. I have been looking into py2.exe to compile it for any windows system. But all the documentation is on python 2.5. I have 2.6 and it wont let me install it. Do you or anyone else have any suggestions. Thanks everyone for all your help. This is what my final program looks like.
import os
if os.path.exists("test2.ext"):
delete = raw_input("File already exists would you like to delete?(Y,N)")
if delete.lower() == "y":
os.remove("test2.ext")
else:
exit()
for lines in open("test.ext"):
lines=lines.strip() #strip newline
x,y = lines.split(":") # now x contains xxxxxx, and y contains yyyyyy-zzzzz
y = y.replace("-",";") + ';' # replace all - to ;
print y,
open("test2.ext","a").write(y)
raw_input("\n\nPress enter to exit")Thanks in advance for all the help.

hi
you must use the < pre > and < /pre > tags.when you do "print y," (<-- note the comma after the "print y") , print doesn't print a new line. Therefore, remove the comma to print newline.
also, open(file).write(y) doesn't write a newline. So you should do open(file).write(y+"\n") since the newlines are already "stripped" as you go through the loop. you have to put them back when you write to file

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |