Generating text files (newbie)
Assume I have a tab-delimited text file called foo.txt organized as follows: x11 -0.04 x22 -0.42 x33 0.3 My goal is to read in this file and use the information therein to output a new file that is organized as follows: x11 IRM=3PL IPB= -0.04 x22 IRM=3PL IPB= -0.42 x33 IRM=3PL IPB= 0.3 I am a newbie with python and am trying to put the pieces together. Now, I know if I had a list, I could do something like: a = [-0.04, -0.42, 0.3] >>> for i in a: print "IRM = 3PL", "\t", "IPB = ", i IRM = 3PL IPB = -0.04 IRM = 3PL IPB = -0.42 IRM = 3PL IPB = 0.3 And then I could write this to a file. But I am not sure how to I might tackle this when I read in a text file. So far, my basic skills allow for me to accomplish the following # read in file params = open('foo.txt', 'r+') # Write to the file params.write('Add in some new test\n') params.close() Any pointers are appreciated. I'm using python 2.3 on a windows xp machine. Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Beginner question on text processing
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 -- http://mail.python.org/mailman/listinfo/python-list
RE: Wow, Python much faster than MatLab
R is the open-source implementation of the S language developed at Bell laboratories. It is a statistical programming language that is becoming the de facto standard among statisticians. Rpy is what allows an interface between python and the R language. Harold > -Original Message- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] On > Behalf Of Stef Mientki > Sent: Saturday, December 30, 2006 9:24 AM > To: python-list@python.org > Subject: Re: Wow, Python much faster than MatLab > > Mathias Panzenboeck wrote: > > A other great thing: With rpy you have R bindings for python. > > forgive my ignorance, what's R, rpy ? > Or is only relevant for Linux users ? > > cheers > Stef > > > So you have the power of R and the easy syntax and big > standard lib of > > python! :) > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Output to csv
I've hacked together a small utility program that I assume can be written much better. The program reads in output from a statistical program and spits out the relevant data needed. The purpose of the program is to reach into the original data file (which is a text file), pull out the relevant informaiton, and spit out a file that can be opened in Excel in order to create tables for some reports we need. To accomplish this, I enter in commas by brute force so that the output is a csv file that excel can open. This program works fine, but, as I am still learning python, I am interested in ways to write better code. If anyone has any suggestions, they are most appreciated. Below is the program in its current form. Harold filename = raw_input("Please enter the WinSteps ISF file: ") new_file = raw_input("Enter the name of the csv file to output: ") # create a new file defined by the user f = open(new_file, 'w') f.write("Num, Measure, SE, Measure, SE, Measure, SE, Measure, SE \n") params = open(filename, 'r') for line in params: x = line if x[15] == '1': print >> f, x[4:6], ',' ,x[39:47], ',' ,x[49:55], ',,,' elif x[15] == '2': print >> f, x[4:6], ',' ,x[39:47], ',' ,x[49:55], ',' ,x[90:97], ',' ,x[99:105], ',,' elif x[15] == '3': print >> f, x[4:6], ',' ,x[39:47], ',' ,x[49:55], ',' ,x[90:97], ',' ,x[99:105], ',' ,x[140:147], ',' ,x[149:155], ',' elif x[15] == '4': print >> f, x[4:6], ',' ,x[39:47], ',' ,x[49:55], ',' ,x[90:97], ',' ,x[99:105], ',' ,x[140:147], ',' ,x[149:155], ',',x[190:197], ',' ,x[199:205] f.close() -- http://mail.python.org/mailman/listinfo/python-list
Checking if elements are empty
Dear list: Suppose I have a string as follows x = ' \t'ff' I can split this up as y = x.split('\t') Which gives [ ' ', 'ff'] len(y) 2 Is there a way to check if the first element of y is null? -- http://mail.python.org/mailman/listinfo/python-list
Python and xml
I am a python neophyte who has used python to parse through text files using basic functions and no OOP experience. I have a need to process some xml files and from what I am now reading python is the exact tool I need to work through this issue. However, as I read online docs and peruse which books to buy, I am quickly becoming overwhelmed with the topic and could use some guidance on how to best tackle my task. To frame the issue, here is what I would like to be able to do. I have a statistical program that outputs the data into an xml document. My goal is to use python to parse this document and output a .txt document or .csv document that organizes the results into human-readable rows and columns in a nicely formatted manner. As I read online docs about xml and python, it seems the topic is very big. While I am more than happy to invest significant time learning this topic, it may be possible to narrow the scope of what I need to know and be able to do in order to complete my task. Any suggestions on how I should proceed and/or fundamentals I need to learn? Is anyone aware of simple how to docs or other tutorials that demonstrate this kind of task? Thanks, Harold -- http://mail.python.org/mailman/listinfo/python-list
Elementtree to parse xml
I received some help on list with sample code on how to parse through an xml file using element tree. I now have this working using toy examples nicely. However, when I work to apply this to my actual xml file, I am hitting a roadblock. Is anyone on the list able to look at an xml file that I can send as well as my python code to offer suggestions? I would normally insert a minimal, commented example inside this email. But the xml file is a bit too big to put in this email. I am *very* close to having this resolved, but just need a nudge in the right direction. Thanks, Harold -- http://mail.python.org/mailman/listinfo/python-list
Process multiple files
Say I have multiple text files in a single directory, for illustration they are called "spam.txt" and "eggs.txt". All of these text files are organized in exactly the same way. I have written a program that parses each file one at a time. In other words, I need to run my program each time I want to process one of these files. However, because I have hundreds of these files I would like to be able to process them all in one fell swoop. The current program is something like this: sample.py new_file = open('filename.txt', 'w') params = open('eggs.txt', 'r') do all the python stuff here new_file.close() If these files followed a naming convention such as 1.txt and 2.txt I can easily see how these could be parsed consecutively in a loop. However, they are not and so is it possible to modify this code such that I can tell python to parse all .txt files in a certain directory and then to save them as separate files? For instance, using the example above, python would parse both spam.txt and eggs.txt and then save 2 different files, say as spam_parsed.txt and eggs_parsed.txt. Thanks -- http://mail.python.org/mailman/listinfo/python-list
Learning Tkinter
I am currently reading An Intro to Tkinter (1999) by F. Lundh. This doc was published in 1999 and I wonder if there is a more recent version. I've googled a bit and this version is the one I keep finding. I like how this document is organized and also how it provides the code with visuals of what should appear on the screen. If there are other docs I should read, please let me know. Second, I am trying to work through a couple of the examples and make some small tweaks as I go to see how new things can work. In the first case, I have copied the code in the book to see how the menu works and are created as in the example menu.py below. I see how menus are created and how the command option is used to call the function callback. # menu.py from Tkinter import * def callback(): print "called the callback!" root = Tk() # create a menu menu = Menu(root) root.config(menu=menu) filemenu = Menu(menu) menu.add_cascade(label="File", menu=filemenu) filemenu.add_command(label="New", command=harold) filemenu.add_command(label="Open...", command=callback) filemenu.add_separator() filemenu.add_command(label="Exit", command=callback) helpmenu = Menu(menu) menu.add_cascade(label="Help", menu=helpmenu) helpmenu.add_command(label="About...", command=callback) mainloop() However, I now want to incorporate a basic python program with a command. Say I have a simple program called test.py # test.py filename = raw_input("Please enter the file you want to open: ") new_file = raw_input("Save the output file as: ") f = open(new_file, 'w') new = open(filename, 'r') for line in new: x = line.split('\t') print >> f, x[0],':', x[1] f.close() To make this example complete assume I have a text file like this # data.txt 1 one 2 two 3 three 4 four So, the user currently just follows directions on the screen, enters the file names, and I get what I want. I'd like to try experimenting with gui programming to see if the python programs I have written can be made even more user friendly. I currently use py2exe to create executables so that others in my organization can use these programs. In that spirit, say I want to have a menu option that allows the user to search their computer for this file, execute the python code and then save the result as a user-defined filename. So, I guess my questions are how do I associate the portion of code in menu.py "filemenu.add_command(label="Open...", command=callback)" with an operation that gives the user the ability to search the drives on their machine and then once they do let python execute the code in test.py? Many thanks, -- http://mail.python.org/mailman/listinfo/python-list
Making Windows Larger in Tkinter
Thanks to some help I received on list the other day, I now have a very nice windows-based application that implements multiple programs. This is a very powerful tool. Now I am working to make this window pretty. One problem I cannot find help on is how to make the windows a certain size. For example, I have added in a root.title() with a lot of text in the file example.py below. Is it possible to make the window box a specific size, or by default, always be large enough to show the entire text in root.title? example.py from Tkinter import * import tkMessageBox def callback(): print "called the callback!" def message(): if tkMessageBox.askokcancel("Quit", "Do you really wish to quit?"): root.destroy() def about(): tkMessageBox.showinfo("About", "Foo?") root = Tk() root.title('Hello World: How Can we see the entire title?') # create a menu menu = Menu(root) root.config(menu=menu) filemenu = Menu(menu) menu.add_cascade(label="File", menu=filemenu) filemenu.add_command(label="New", command=message) filemenu.add_command(label="Open...", command=callback) filemenu.add_separator() filemenu.add_command(label="Exit", command=message) helpmenu = Menu(menu) menu.add_cascade(label="Help", menu=helpmenu) helpmenu.add_command(label="About...", command=about) mainloop() -- http://mail.python.org/mailman/listinfo/python-list
Use python to execute a windows program
Dear list: My question is conceptual at the moment. Current problem: I have a windows-based program that reads in a file and from that file generates data that is saved to a file. The way we do this now is a person sits in front of their machine and proceeds as follows: 1) Open windows program 2) Click file -> open which opens a dialog box 3) Locate the file (which is a text file) click on it and let the program run. This would be no problem if we did this for a small number of files. But, we repeat this process for a few hundred files. So, the human is sitting in front of the machine repeating this process for days until we have all data generated. Question: Is it possible to write a python script that would automate this process such that it could interact with the windows program, loop through all of the text files in a directory rather than the human repeating this process for days. I have sued python quite a bit to parse XML and text files, but I have never used it such that it would interact with a program designed to work in windows. So, I'm not sure if such a problem is conceptually possible. I wish I could provide minimal commented code, but I am not sure where to even start with this problem other than to first ask if it is conceptually possible. -- http://mail.python.org/mailman/listinfo/python-list
RE: Use python to execute a windows program
I am working with this now. I'm toying with the examples to test out a few things and learn how this works. I've made some modifications such that I have the following working (below). This does nothing more than open a program. I have commented out the portion #app.AM.MenuSelect("File->Open Database") When it is uncommented, the program fails. However, when I tinker with this MenuSelect() for, say, Notepad, this presents no problem and behaves as expected. For example, the following works with notepad: app.Notepad.MenuSelect("Help->Help Topics") At the risk of sounding too silly, how do I know what to place after app.??.MenuSelect? I've tried this with a few programs and the name I use in place of ?? Doesn't seem to work. import time import sys try: from pywinauto import application except ImportError: import os.path pywinauto_path = os.path.abspath(__file__) pywinauto_path = os.path.split(os.path.split(pywinauto_path)[0])[0] import sys sys.path.append(pywinauto_path) from pywinauto import application def AM(): app = application.Application() try: app.start_( # connect_(path = ur"C:\Program Files\American Institutes for Research\AMBeta\AM.exe") except application.ProcessNotFoundError: print "You must first start Windows Media "\ "Player before running this script" sys.exit() #app.AM.MenuSelect("File->Open Database") def Main(): start = time.time() AM() if __name__ == "__main__": Main() > -Original Message- > From: simon.brunn...@gmail.com > [mailto:simon.brunn...@gmail.com] On Behalf Of Simon Brunning > Sent: Friday, September 11, 2009 11:02 AM > To: Doran, Harold > Cc: python-list@python.org > Subject: Re: Use python to execute a windows program > > 2009/9/11 Doran, Harold : > > The way we do this now is a person sits in front of their > machine and > > proceeds as follows: > > > > 1) Open windows program > > 2) Click file -> open which opens a dialog box > > 3) Locate the file (which is a text file) click on it and let the > > program run. > > It might very well be possible, depending upon how the > program you want to automate has been written. > > First, make sure, absolutely sure, that's there's no "proper" > automation option available - a command line version, COM > automation, that kind of thing. These approaches are very > much easier than GUI automation. > > If none of these options are available, > <http://pywinauto.openqa.org/> is probably what you need. > > -- > Cheers, > Simon B. > -- http://mail.python.org/mailman/listinfo/python-list
RE: Use python to execute a windows program
Thanks, Jerry. Tried that, as well as various other possible names to no avail. > -Original Message- > From: python-list-bounces+hdoran=air@python.org > [mailto:python-list-bounces+hdoran=air@python.org] On > Behalf Of Jerry Hill > Sent: Friday, September 11, 2009 3:09 PM > To: python-list@python.org > Subject: Re: Use python to execute a windows program > > On Fri, Sep 11, 2009 at 12:46 PM, Doran, Harold > wrote: > > I am working with this now. I'm toying with the examples to > test out a > > few things and learn how this works. I've made some > modifications such > > that I have the following working (below). This does > nothing more than > > open a program. > > > > I have commented out the portion > > > > #app.AM.MenuSelect("File->Open Database") > > > > When it is uncommented, the program fails. However, when I > tinker with > > this MenuSelect() for, say, Notepad, this presents no problem and > > behaves as expected. For example, the following works with notepad: > > > > app.Notepad.MenuSelect("Help->Help Topics") > > > > At the risk of sounding too silly, how do I know what to > place after > > app.??.MenuSelect? I've tried this with a few programs and > the name I > > use in place of ?? Doesn't seem to work. > > I'm not very familiar with pywinauto myself, but a quick look > through the docs says that the application looks for a window > or dialog with a "similar" name to what you put there. So, > what does the title bar of the window opened by AM.exe say? > You should use a name that is "similar" to the title of the > window you're trying to control. > > -- > Jerry > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list