Re: command-line one-liners a la Perl?
On Jun 18, 9:36 am, kj wrote: > I'm a recovering Perl addict, and I'm jonesin' badly for command-line > one-liners, like > > % perl -lne '@f=split "\t";print join "\t",@f[3,1] if $f[2]=~/frobozz/i' > in.txt > > How can I get my fix with Python? > > kynn I'd encourage you to learn the ways of Python which ordinarily don't encourage cramming lots of code into a single line. However... if you are insistent on seeing how much rope Python can give you to hang yourself... ;) $ python -c 'print "\n".join( "\t".join( ( data[3], data[1] ) ) for data in ( lambda fn : ( line.strip().split("\t") for line in file( fn, "r" ) ) )( "in.txt" ) if data[2].lower().find( "frobozz" ) > -1 )' (untested) should do something similar to your perl statement (display the fourth and second fields from tab-delimited lines containing "frobozz" in any case-mixture in the third field for lines read from the "in.txt" file). Note that the frobozz search is not a regex search in this example. Requiring regex would make things more complicated. Probably could be cleaned up a bit. u. -- http://mail.python.org/mailman/listinfo/python-list
Re: Why is it that str.replace doesn't work sometimes?
On Jun 24, 12:11 pm, humn wrote: > but this doesn't: > > if '\title' in line: > line = line.replace('\title{', '[size=150][b]') > line = line.replace('}', '[/b][/size]') \t is an escaped character. so, '\title' will look for 'itle' Two ways to fix this: 1. use r'\title' 2. use '\\title' \c does not represent a known escape sequence so it remains two characters. -- http://mail.python.org/mailman/listinfo/python-list
Re: Reading then sending new parts of a log file
On Jun 24, 10:23 am, Chuck Connors wrote: > Hey guys. I'm trying to work up a little program that will send any > new lines written to a file (log file from my home automation > software) to me via instant message. I've gotten the instant message > sending part figured out using xmpppy. > > I've done a few things with Python in the past but I am in no means > more than a beginner/hacker. Can someone tell me what commands/ > modules/etc exist for monitoring and parsing a file for new > information that I can then send to my IM sending function? I am not > asking for anyone to write the code but merely a push in the right > direction. Here's a little nudge: http://code.activestate.com/recipes/157035/ In place of its "print" line, you'd make your call into your existing code to send the message. Some of the comments offer some improvements on it (always read the comments on the recipes), but this is likely the direction you will want to head. -- http://mail.python.org/mailman/listinfo/python-list
Re: ElementTree.XML(string XML) and ElementTree.fromstring(string XML) not working
On Jun 25, 9:02 pm, Kee Nethery wrote: > Summary: I have XML as string and I want to pull it into ElementTree > so that I can play with it but it is not working for me. XML and > fromstring when used with a string do not do the same thing as parse > does with a file. How do I get this to work? > > Details: > I have a CGI that receives XML via an HTTP POST as a POST variable > named 'theXml'. The POST data is a string that the CGI receives, it is > not a file on a hard disk. > > The POSTed string looks like this when viewed in pretty format: [...] > et.parse seems to pull in the entire XML document and give me > something to play with whereas et.XML and et.fromstring do not. > > Questions: > How do I get this to work? > Where in the docs did it give me an example of how to make this work > (what did I miss from reading the docs)? > [skipping bonus points question] I'm not sure what you're expecting. It looks to me like things are working okay: My test script: import xml.etree.ElementTree as ET data=""" Autumn 1 8.46 YES 19 Any Street Berkeley California 12345 People's Republic of Berkeley Jon Roberts ju...@shrimp.edu """ xml = ET.fromstring( data ) print xml print "attrib ", xml.attrib print "tag ", xml.tag print "text ", xml.text print "contents " for element in xml : print element print "tostring" print ET.tostring( xml ) when run, produces: attrib{} tag xml text contents tostring Autumn 1 8.46 YES 19 Any Street Berkeley California 12345 People's Republic of Berkeley Jon Roberts ju...@shrimp.edu Which seems to me quite useful (i.e. it has the full XML available). Maybe you can explain how you were trying to "play with" the results of fromstring() that you can't do from parse(). The documentation for elementtree indicates: > The ElementTree wrapper type adds code to load XML files as trees > of Element objects, and save them back again. and > The Element type can be used to represent XML files in memory. > The ElementTree wrapper class is used to read and write XML files. In the above case, you should find that the getroot() of your loaded ElementTree instance ( parse().getroot() ) to be the same as the Element generated by fromstring(). -- http://mail.python.org/mailman/listinfo/python-list