I’m trying to clear the Entry widget after the user presses a button using Tkinter. I tried using ent.delete(0, END), but I got an error saying that strings don’t have the attribute delete. Here is …
Most recent previous business day in Python
I need to subtract business days from the current date. I currently have some code which needs always to be running on the most recent business day. So that may be today if we’re Monday thru Friday, …
How to use glob() to find files recursively?
This is what I have: glob(os.path.join(‘src’,’*.c’)) but I want to search the subfolders of src. Something like this would work: glob(os.path.join(‘src’,’*.c’)) glob(os.path.join(‘src’,’*’,’*.c’)) …
Hiding axis text in matplotlib plots
I’m trying to plot a figure without tickmarks or numbers on either of the axes (I use axes in the traditional sense, not the matplotlib nomenclature!). An issue I have come across is where matplotlib …
Getting next element while cycling through a list
li = [0, 1, 2, 3] running = True while running: for elem in li: thiselem = elem nextelem = li[li.index(elem)+1] When this reaches the last element, an IndexError is raised (as is …
How to convert an XML string to a dictionary?
I have a program that reads an XML document from a socket. I have the XML document stored in a string which I would like to convert directly to a Python dictionary, the same way it is done in Django’s simplejson library. Take as an example: Then dic_xml would look like {‘person’ : { ‘name’ : ‘john’, ‘age’ : 20
Splitting a list into N parts of approximately equal length
What is the best way to divide a list into roughly equal parts? For example, if the list has 7 elements and is split it into 2 parts, we want to get 3 elements in one part, and the other should have 4 …
Split a string at newline characters
I have a string, say a = “Show details1nShow details2nShow details3nShow details4nShow details5n” How do we split the above with the delimiter n (a newline)? The result should be …
real time subprocess.Popen via stdout and PIPE
I am trying to grab stdout from a subprocess.Popen call and although I am achieving this easily by doing: I would like to grab stdout in “real time”. With the above method, PIPE is waiting to grab all the stdout and then it returns. So for logging purposes, this doesn’t meet my requirements (e.g. “see” what is going on while
How to read specific lines from a file (by line number)?
I’m using a for loop to read a file, but I only want to read specific lines, say line #26 and #30. Is there any built-in feature to achieve this? Answer If the file to read is big, and you don’t want to read the whole file in memory at once: Note that i == n-1 for the nth line.