Computing.Net > Forums > Programming > python help please

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

python help please

Reply to Message Icon

Name: mustang509
Date: October 3, 2008 at 19:54:01 Pacific
OS: vista
CPU/Ram: 1250 mb
Product: pavilion
Comment:

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,



Sponsored Link
Ads by Google

Response Number 1
Name: ghostdog
Date: October 4, 2008 at 00:05:21 Pacific
Reply:

I have replied to your previous post. If you want to write to file,


c:\test> python script.py > outputfile

this is provided you only want 1 output file. If you need to write to many files, do it inside Python.
Read the docs.


0

Response Number 2
Name: mustang509
Date: October 7, 2008 at 07:54:39 Pacific
Reply:

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.


0

Response Number 3
Name: mustang509
Date: October 7, 2008 at 09:15:34 Pacific
Reply:

Sorry not sure why its not putting the spaces in my program like they should be.


0

Response Number 4
Name: ghostdog
Date: October 9, 2008 at 06:40:34 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: python help please

Assembly Language Help please? www.computing.net/answers/programming/assembly-language-help-please/4705.html

help please www.computing.net/answers/programming/help-please/8283.html

Help please www.computing.net/answers/programming/help-please/10538.html