Re: [Tutor] xml parsing without a root element
Can you encapsulate the contents of the file in a root element before processing? - Original Message - From: rail shafigulin To: tutor@python.org Sent: Tuesday, August 30, 2011 10:27 AM Subject: [Tutor] xml parsing without a root element hello everyone. i need to parse a an xml-like file. the problem that i'm facing is that this file doesn't have the root element but in all other terms it is the same as xml, i.e tag1 /tag1 tag2 /tag2 tag3/ does anybody know if there is a module in python that allows to process an xml file without a root element? i tried ElementTree but it didn't work. any help is appreciated thanks -- ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] STUPID telnet connection question
I think Steven and Alan misunderstood the Rayon's question. Rayon is using his telnet script to pass commands to a device ONE AT A TIME. Then he breaks the connection and reconnects for the next command.. He is asking how to open a telnet connection, pass MULTIPLE commands, then close the session. In my opinion, both of your responses border on rude. This mailing list is supposedly for beginners who often don't even know enough to formulate a clear question. If some of the more senior members ridicule the beginner's stupid questions, how will he or anyone else learn anything. If I were a shy beginning programmer, these responses would probably cause me to think twice before asking any stupid questions. Why put myself out there for ridicule? There are no stupid questions! There are only stupid teachers. - Original Message - From: Steven D'Aprano st...@pearwood.info To: tutor@python.org Sent: Friday, June 10, 2011 3:53 AM Subject: Re: [Tutor] telnet connection question Rayon wrote: HI All, Is there any way that I can use python telnetlib to connect to a telnet session. It would be a pretty rubbish telnet library if it didn't let you make telnet connections. I don't understand why you are asking this question, since you have successfully made connections and are complaining that they are too slow. Send commands and get back data without closing the connection. It would be a pretty rubbish telnet library if you couldn't send telnet commands. What makes you think it might not? I need the response to be faster and the login process is taking up too much time. Then get a faster link. Seriously, this is a silly question. It's like saying I need a longer piece of string. Okay, fine. How long would you like? Why is the piece you have too short? We can't answer those questions. How are you logging in? How much time are you talking about? How fast is your link? I was thinking I could use a queue to pass in data but I am not sure how i would get it out. What data do you need to pass in, and why do you think a queue will help you? -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] search-replace
Or, open the file as a blob (one long string) and do a single 'replace'. fin = open(dirtyfile.txt, 'r').read().replace('## ', '#') open(dirtyfile.txt, 'w').write(fin) or, open(dirtyfile.txt, 'w').write(open(dirtyfile.txt, 'r').read().replace('## ', '#')) - Original Message - From: Tommy Kaas tommy.k...@kaasogmulvad.dk To: tutor@python.org Sent: Monday, June 06, 2011 2:58 AM Subject: Re: [Tutor] search-replace -Oprindelig meddelelse- Fra: tutor-bounces+tommy.kaas=kaasogmulvad...@python.org [mailto:tutor-bounces+tommy.kaas=kaasogmulvad...@python.org] På vegne af Alan Gauld Sendt: 6. juni 2011 11:51 Til: tutor@python.org Emne: Re: [Tutor] search-replace Tommy Kaas tommy.k...@kaasogmulvad.dk wrote I'm especially interested to know how I do more than just one search-replace without having to repeat the whole step below. fin = open(dirtyfile.txt) fout = open(cleanfile.txt, w) for line in fin: fout.write(line.replace('## ', '#')) fin.close() fout.close() Can be simplified to: with open(cleanfile.txt, w) as fout: for line in open(dirtyfile.txt): fout.write(line.replace('## ', '#')) To do multiple replaces simply expand the inner block for line in open(dirtyfile.txt): line = line.replace() #first line = line.replace() #second etc fout.write(line) Or if you have a lot of them: replacePatterns = [('##','#'),('!!!','!!'),] for line in open(dirtyfile.txt): for old,new in repace_patterns: line = line.replace(old,new) fout.write(line) Excellent! Thanks. tommy ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Making a script part of the terminal
I think I understand. One thing you can do is create a myprogram.bat file with an entry something like: python c:/myprogrampath/myprogram.py Put the myprogram.bat file in the root directory or any directory in the PATH. Then it will behave like an executable. In Linux, it would be myprogram.sh with the executable bit set. - Original Message - From: michael scott To: tutor@python.org Sent: Friday, May 20, 2011 11:10 AM Subject: Re: [Tutor] Making a script part of the terminal Thank you for the reply, but thats not exactly what I mean. Perhaps I should say, how do I install a program to my computer, so that I can use it by its self without running it with python. No matter what directory I'm in I can type mozilla in and it runs, no matter what directory I'm in if I type sudo natutilus it will run, no matter what directory I'm in if I type gedit it will run. So I'm trying to achieve this with the script I wrote. I don't know the terminology to ask the question correctly, so forgive me. What is it about you... that intrigues me so? -- From: James Reynolds eire1...@gmail.com To: michael scott jigenbak...@yahoo.com Cc: tutor@python.org Sent: Fri, May 20, 2011 1:57:57 PM Subject: Re: [Tutor] Making a script part of the terminal We just had a similar question yesterday. Just make sure Python is on your PATH. CD to the directory where your file is located and then you can just type python myfile.py where myfile is the name of your file. On Fri, May 20, 2011 at 1:43 PM, michael scott jigenbak...@yahoo.com wrote: Okay, my title might be undescriptive, let me try to explain it better. I want to take a script I've written and make it usable by typing its name in the terminal. Perfect example is the python interpreter. You just type in the word python to the terminal and then the interpreter runs. I know other programs can do this as well (like mozilla or nautilus or rhythmbox). So how do I make my scripts executable from the terminal? What is it about you... that intrigues me so? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -- ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] create an xls file using data from a txt file
I'm not contradicting anyone, just relating my experience. I have a large suite of Python programs that run routinely on both Windows and Linux systems. Some of the programs build large directory tree structures. I cast all directory delimiters to the forward slash /. No problems. - Original Message - From: Steve Willoughby st...@alchemy.com To: tutor@python.org Sent: Wednesday, May 11, 2011 12:48 PM Subject: Re: [Tutor] create an xls file using data from a txt file On 11-May-11 12:14, James Reynolds wrote: Actually, I never knew that about the windows separators, since I've just always used the '\' out of habit. If you want your code to run everywhere, you should use the functions in os.path to manipulate and build paths. Otherwise, using \ all the time means your code will ONLY ever work on Windows. Using / all the time means your code will work fine on Mac OS X, Linux, or other POSIX systems, and PROBABLY ok on Windows most of the time, but not on other systems. out path = 'C:\\test.xls', which will be 'C:\test.xls' or you can write out path = r'C:\test.xls' the r bit tells python that the following is a regular expression. or regex. Not to be too pedantic, but since this is a tutorial list, I'll point out the more accurate answer so new programmers don't get a mistaken impression. The 'r' string prefix does not actually mean regular expressions. It means raw string where (almost) no backslash codes are recognized in the string constant, so you could say r'C:\test.xls' instead of 'c:\\test.xls'. Raw strings are, however, really useful for strings which hold regular expressions, so you see them in that context a lot. ... ah... and I just noticed that this was pointed out later in the thread. Sorry for the repeat there. ‘/’ is perfectly valid Windows separator. See the *tested* examples below. It works just fine pretty much anywhere I have ever tried it, including the command line. (except apparently for an MSOffice file save dialog that I tried just now) Not... quite. / is accepted by a number of programs, including the Python interpreter, which came from Unix-like systems where / is the directory separator. Very old versions of MSDOS could be configured to use / on the command line for pretty much everything, but that has been deprecated for a long time now (they originally wanted / for command-line switches instead, so used \ for directory separators). Core windows commands don't generally accept it, including native Windows applications (although sometimes they're lenient in what they accept). It'll work for command-line Python script usage because it's *python* that allows them, not *windows*. -- Steve Willoughby / st...@alchemy.com A ship in harbor is safe, but that is not what ships are built for. PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Defining Exceptions
It's difficult to see exactly what the data looks like as it is received. Where are the line breaks? At the commas? Is the header just the first line? Is 1Time considered part of the header? Is everything after 1Time considered data? I can see several simple alternatives to your method. I don't think you need the exception code. I think you can replace this: lines = resp.split('\r\n') waste=lines.pop() while True: try: lines.remove(waste) except Exception: ?? with this: lines = resp.splitlines[:2] This part, I don't understand: for x,row in enumerate(lines): lines[x]=row.split(',') - Original Message - From: Tom Brauch To: tutor@python.org Sent: Wednesday, February 02, 2011 6:51 AM Subject: [Tutor] Defining Exceptions All, I am a python neophyte and not terrible well versed in programming (as will become obvious shortly) I have a script which is reading a serial device on a schedule. The device outputs a header at the beginning of every read. I have a data file which I am appending and would like to eliminate the header so that I end up with one master data file. A copy of the serial readout is: * 6CSV Type Reports2 - Display All Data3 - Display New Data4 - Display Last Data5 - Display All Flow Stats6 - Display New Flow Stats7 - Display All 5-Min Flow8 - Display New 5-Min Flow9 - Display Error Log 4 - Display CSV DataStation, 1Time,Conc(mg/m3),Qtot(m3),no(V),WS(MPS),no(V),RH(%),no(V),AT(C),E,U,M,I,L,R,N,F,P,D,C,T,02/02/11 08:00, 0.042, 0.701, 0.004, 0.1, 0.002, 7, 0.012, -18.0,0,0,0,0,0,0,0,0,0,0,0,0, I am only interested in the information following the T, in the header. I have tried to use an exception to have the script throw out all data prior to the T, in the header but I don't know how to define this excecption. My current script looks as follows: def cycle(ser,prefix): inRecovery=False resp=False #tm.sleep(30.0) ser.open() tm.sleep(2) ser.write('\n\r\n\r\n\r') resp=buffer_read(ser) ser.write('6') resp=buffer_read(ser) ser.write('3') resp=buffer_read(ser) lines = resp.split('\r\n') waste=lines.pop() while True: try: lines.remove(waste) except Exception: ?? for x,row in enumerate(lines): lines[x]=row.split(',') if DEBUG: print lines f=open(prefix,'a') csvf = csv.writer(f) csvf.writerows(lines) f.close() ser.close() How do I define the exception so that I get only the data following the header? Thanks! Tomb -- ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] print Hello, World!
Seven years ago, my story was similar. I started off with The Python Quick Book (Manning) and Python - Visual Quickstart Guide (Peachpit Press). Both are very easy to follow. I still pick up the Quick book once in a while for reference. This Tutor list helped a lot. I learned by trying out the things people offered as solutions to problems from people like you and me. Asking questions here is a way to help a lot of new Python programmers, and a few older ones, too. There are a lot more resources these days, too. Search on Python in YouTube. There are a lot of on-line tutorials, too. - Original Message - From: Doug Marvel smokeinourlig...@gmail.com To: tutor@python.org Sent: Wednesday, February 02, 2011 6:00 PM Subject: [Tutor] print Hello, World! Hey folks, I'm Doug. I've been using computers since second grade, and I know a little about them. I am, however, completely new to programming. I don't even know what I know about it. I'd like some social interaction with this, but I can't go back to school until summer or fall of this year. I don't want to wait to start learning this as I feel like I'm already about a million years behind. I asked the Oracle (www.google.com) and after messing around with the Python Shell and getting a lot of error messages, I decided I need some remote help. Here's where I'm at: - I have downloaded and installed Python 2.6.4. Successfully, I think. - I am running Windows XP SP3 (though I'm going to see if I can do this on my laptop, which has Windows 7) - I have toyed around with some tutorials, but all they really taught me is that I need a teacher. I'm sure you guys are busy, but I read that the most basic questions are okay. As I'm sure there is at least one good resource on the net for people in my position, I'd like some suggestions on where to start. I plan on bothering you all as little as possible, but I am seriously hoping to make real progress between now and my first class. I have a feeling once I get a basic understanding, I'll run away with it. It's just very... big right now. So this list seems like a good thing, but tell me if I'm in the wrong place. I am hoping for a link to a somewhat comprehensive online resource that explains from the beginning in English, plain English, as this is the only language I speak. Something to get my foot in the door would be awesome. Cheers, Doug Marvel ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Interactive visualization in python
FYI... There is a non-Python commercial program called XMLSpy which displays a visual tree rendition of an XML schema (.xsd) file. The schema file can be created or manipulated with Python/ElementTree. Maybe it can help you in your program development. - Original Message - From: Aravind Venkatesan To: tutor@python.org Sent: Saturday, November 06, 2010 3:56 PM Subject: [Tutor] Interactive visualization in python Hello, This is Aravind. I am a university graduate student. I am looking for a software module or package to visualize a hierarchial tree data structure in python. Here's the problem: I have a tree(hierarchially represented) with set of nodes and edges. I would like to visualize this tree first. Then i would like to have each node a clickable object so that when a node in the tree is clicked using a mouse, i want to show some data associated with that node(probably a graph) in another popup window. What kind of packages exists in python which will help me solve this? Regards, Aravind -- ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] join question
join operates on lists. It joins the elements of the list using the leading character or string as the delimiter. In this case it is NUL. Try putting a character or string, like 'XX\n' in front of the .join in both places. It should illustrate what's really happening. XX\n.join(...) In the original case, join is presented with a list. In the second case, join is presented with a string. But Python happily converts it to a list of characters. - Original Message - From: Roelof Wobben rwob...@hotmail.com To: tutor@python.org Sent: Thursday, October 14, 2010 6:50 AM Subject: [Tutor] join question Hello, I found this answer to a problem for me : print ''.join([zf.getinfo('%s.txt' % p).comment for p in zpp]) So I thought that this would be the same : for p in zpp: test = zf.getinfo(p).comment print ''.join(test) But it seems not to work Can anyone explain why not ? Roelof ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What's the best way to ask forgiveness here?
I suggest something like: try: os.makedirs(path) except: pass open(%s/%s % (path, filename), 'w').write(filedata) - Original Message - From: Emile van Sebille em...@fenx.com To: tutor@python.org Sent: Monday, September 13, 2010 11:56 AM Subject: Re: [Tutor] What's the best way to ask forgiveness here? On 9/13/2010 11:31 AM Brian Jones said... I've been coding Python long enough that 'asking forgiveness instead of permission' is my first instinct, but the resulting code is sometimes clumsy, and I wonder if someone can suggest something I'm missing, or at least validate what's going on here in some way. What I'm trying to do is write a file to a directory. However, the directory may not exist the first time I try to write a file there, so I'm going to first try to write the file, and if I get an exception, create the directory (er, *try* to), and *then* write the file there. Here's my first shot at the code: try: self.save_file(picfile_fullpath, picdata) except IOError as err: # directory doesn't exist. Try to create it. try: os.makedirs(picfile_fullpath) except OSError as oserr: logging.error(Can't create file path: %s (%s) % (picfile_fullpath, oserr)) else: # Created dir, now write file. try: self.save_file(picfile_fullpath, picdata) except IOError as err: logging.error(Bailing. Couldn't save file %s (%s) % (picfile_fullpath, err)) return False Unless this is in a tight loop, I think I'd write: try: os.makedirs(picfile_fullpath) try: self.save_file(picfile_fullpath, picdata) return True # all/only error handling code follows. except IOError as err: logging.error(Bailing. Couldn't save file %s (%s) % (picfile_fullpath, err)) return False except OSError as oserr: logging.error(Can't create file path: %s (%s) % (picfile_fullpath, oserr)) return False which eliminates the duplicated save_file step. Doesn't this seem less readable than the 'ask permission' equivalent? I think it does, but in this case asking permission for every single operation when the dir will only need to be created a single time (and then may be written to several hundred times) is pretty wasteful. Agreed -- if it's in a loop, you'd want to only check once. Of course, if the directory is eg an nfs share, continued access could be an issue. HTH, Emile I suppose I could set some sentinel variable and check for it in a while loop, but then I need some other scaffolding code to make sure I don't infinitely loop trying to create the directory, and probably some other stuff I'm forgetting, so it strikes me as being just as messy. Is there a clean sort of pattern to apply in instances like this? Thanks. brian ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] SSH session problems with network devices
I have Python code that opens a telnet session with a network device and performs a multitude of tasks. Now, I am trying to establish and maintain an SSH connection as an alternative to telent. My research has found that the SSH implementation some network device manufacturers use is not consistent and the problems I'm having are common. I'm hoping to find someone who has found a solution. I am using Paramiko, but I am not limited to that. I am also exploring Appgate MindTerm http://www.appgate.com/index/products/mindterm/. My Paramiko code works correctly when connecting with a Linux system. But when I connect with a router, the connection is dropped immediately after a command is exectued, so it isn't possible to send a sequence of interrelated commands. For example, I want to change the screen paging value so that complete results from the next command will stream back without stopping for --MORE--. Or I need to change the user level and enter a password followed by a series of commands. I have reviewed relevant postings from on the Paramiko list, but there are no clear solutions. http://www.lag.net/pipermail/paramiko/2010-June/001322.html http://www.lag.net/pipermail/paramiko/2010-June/001320.html http://www.lag.net/pipermail/paramiko/2010-March/001284.html I have tried three methods, unsuccessfully, for establishing and maintaining an SSH connection. My test code is at http://pastebin.ca/1918235. I would appreciate any help. I'm open to a non-Paramiko solution, but it has to plug-and-play with the Python utility code I already have. And I hope to keep it simple. Thank you, David Heiser ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Reading every 5th line
- Original Message - From: nitin chandra nitinchand...@gmail.com To: tutor@python.org Sent: Sunday, August 08, 2010 5:04 AM Subject: [Tutor] Reading every 5th line Hello Everyone, I am to make a small programme for a friend of mine where i am to start reading from 14th (string) from a file and then read every 5th row. ie. in 1st read it reads the 14 row in a File, write to an OUTPUT-1 file Next reads 19th row, write to the OUTPUT-1 file then 24th row,... so on. and the second loop does is reads the 15th line as the first line from same input file and write to OUTPUT-2 file next reads 20th line / row, write to the OUTPUT-2 file I have tried various ways but some how i am ONLY able to do i simple read from one file and write to another. There are 3024 rows / lines PER file and there are 24 such file I need to run the programme on. I really need this on an urgent basis. can some one please help me in this. Thanks in Advance Nitin ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor Try using range(start, end, step). If this is for a class exercise, it would be unfair to say more. If it's not for an assignment, I can show you more. Dave ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Reading every 5th line
- Original Message - From: nitin chandra nitinchand...@gmail.com To: davidheise...@gmail.com Sent: Sunday, August 08, 2010 7:29 AM Subject: Re: [Tutor] Reading every 5th line Thank you all. @Dave - Thank you for the tip. No this is not a class exercise, that is assured. Will let know how much progress i made. Truly, I am still foggy, as to how to include it in code. Thanks Nitin Try using range(start, end, step). If this is for a class exercise, it would be unfair to say more. If it's not for an assignment, I can show you more. Dave This may help you get started. FileNames = [FileName01, FileName02, ..., FileName24] for File in FileNames: List = open(File, 'r').readlines() for Start in [[14, %s-1 % File], [15,%s-2 % File]]: OutList = [] for Line in range(Start[0]-1, 3024, 5): OutList.append(List[Line]) open((%s.txt % Start[1]), 'w').writelines(OutList) P.S. My code writing style isn't very conventional. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Seek suggestions for script for looking up extensions usingC:\assoc
You wouldn't gain much with a Pyton script. You can specify the extension with assoc. Try assoc .zip. But if you want to try it anyway, look at the popen2 module (depreciated) or the subprocess module to extract the data and assign it to a variable that you can parse any way you like. - Original Message - From: Richard D. Moores rdmoo...@gmail.com To: tutor@python.org Sent: Friday, August 06, 2010 6:11 AM Subject: [Tutor] Seek suggestions for script for looking up extensions usingC:\assoc Python 3.1; Windows Vista. I just ran C:\assoc and was amazed to see 658 associations roll by. Here's the top of the list: .001=jZip.file .386=vxdfile .3g2=QuickTime.3g2 .3gp=QuickTime.3gp .3gp2=QuickTime.3gp2 .3gpp=QuickTime.3gpp .7Z=jZip.file .aa=NeroShowTime.Files9.aa .aac=QuickTime.aac .ac3=QuickTime.ac3 .aca=Agent.Character.2 .acf=Agent.Character.2 .acl=ACLFile .acr=IrfanView.DCM .acrobatsecuritysettings=AcroExch.acrobatsecuritysettings .acs=Agent.Character2.2 .acsm=Adobe.ACSMessage .AddIn=VCExpress.AddIn.9.0 .adts=QuickTime.adts .aif=IrfanView.aif .aifc=WMP11.AssocFile.AIFF .aiff=WMP11.AssocFile.AIFF .air=AIR.InstallerPackage I thought it would be convenient to have a Python script I could use to look up unfamiliar file extensions. I assume one is possible, but have no idea where to start, and would appreciate hints and suggestions. Thanks, Dick Moores ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] xml question
I agree with Steven D'Aprano. Keep the code as simple as possible. A simple text file with a variable/value pair on each line is very easy to parse and store in a Dictionary object. You can use any convenient delimiter; =, :, $, ... Nesting items under categories takes just a little more code logic, if you want to do that. I have been doing this for many years. Simple is usually better. - Original Message - From: Steven D'Aprano st...@pearwood.info Why XML? Even though XML is plain text, it is *not* a human writable format, except perhaps for the simplest data. XML is one of those things which has become the in-thing and is used in all sorts of inappropriate places just because everybody else uses XML. Even *supporters* of XML describe themselves as having drunk the XML Kool-Aid. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Searching a text file's contents and comparing them to alist
There are probably return characters at the end of each line from the grocery_list. Try using the String method line.strip(). Or grocery_list.read().splitlines() - Original Message - From: Eric Hamiter ehami...@gmail.com To: tutor@python.org Sent: Wednesday, July 14, 2010 8:46 AM Subject: [Tutor] Searching a text file's contents and comparing them to alist Hi all, New programmer here. This is what I want to do: 1. Open an existing text file named grocery_list.txt, which has one item per line, like so: butter juice bread asparagus magazines ice cream 2. ...and search for these items in a pre-defined list. But I can't seem to get this working. Right now after trying the following: aisle_one = [chips, bread, pretzels, magazines] grocery_list = open(grocery_list.txt, r) for line in grocery_list.readlines(): if line in aisle_one: print success else: print no joy grocery_list.close() I get this: no joy no joy no joy no joy no joy no joy when I'm expecting this: no joy no joy success no joy success no joy Am I close? This seems like it should work but I'm obviously missing something. Thanks, Eric ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Looking for duplicates within a list [SOLVED]
How about this? List = [1, 2, 3, 3, 3, 4, 5, 5] for Item in list(set(List)): print Item, List.count(Item) - Original Message - From: Ken G. To: Steven D'Aprano Cc: tutor@python.org Sent: Friday, June 11, 2010 9:09 AM Subject: Re: [Tutor] Looking for duplicates within a list [SOLVED] Steven D'Aprano wrote: On Sat, 12 Jun 2010 12:58:19 am Alan Gauld wrote: Have you looked at the count method of lists? Something like: counts = set(( item, mylist.count(item)) for item in mylist if mylist.count(item) 1) That's a Shlemiel the Painter algorithm. http://www.joelonsoftware.com/articles/fog000319.html Seems to work... You say that now, but one day you will use it on a list of 100,000 items, and you'll wonder why it takes 45 minutes to finish, and curse Python for being slow. Hee, hee. Will investigate further. Thanks. Ken -- ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor