Beginner question on text processing

Doran, Harold HDoran at air.org
Fri Dec 29 08:20:36 EST 2006


I am beginning to use python primarily to organize data into formats
needed for input into some statistical packages. I do not have much
programming experience outside of LaTeX and R, so some of this is a bit
new. I am attempting to write a program that reads in a text file that
contains some values and it would then output a new file that has
manipulated this original text file in some manner.

To illustrate, assume I have a text file, call it test.txt, with the
following information:

X11	.32
X22	.45

My goal in the python program is to manipulate this file such that a new
file would be created that looks like:

X11	IPB = .32
X22	IPB = .45

Here is what I have accomplished so far.

##### Python code below for sample program called 'test.py'

# Read in a file with the item parameters
filename = raw_input("Please enter the file you want to open: ")

params = open(filename, 'r')

for i in params:
	print 'IPB = ' ,i
# end code

This obviously results in the following:

IPB =  x11	.32
IPB =  x22	.45

So, my questions may be trivial, but:

1) How do I print the 'IPB = ' before the numbers? 
2) Is there a better way to prompt the user to open the desired file
rather than the way I have it above? For example, is there a built-in
function that would open a windows dialogue box such that a user who
does not know about path names can use windows to look for the file and
click on it. 
3) Last, what is the best way to have the output saved as a new file
called 'test2.txt'. The only way I know how to do this now is to do
something like:

python test.py > test2.txt

Thank you for any help
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20061229/766d076e/attachment.html>


More information about the Python-list mailing list