Skip to content

Tag: python

Convert Python ElementTree to string

Whenever I call ElementTree.tostring(e), I get the following error message: Is there any other way to convert an ElementTree object into an XML string? TraceBack: Answer Element objects have no .getroot() method. Drop that call, and the .tostring() call works: You only need to use .getroot() if you have an El…

How do I retrieve a username with Python keyring?

I have a Mercurial keyring on my Windows 7 machine. I am using the Python keyring library to get user credentials from the Mercurial keyring. I can retrieve the password for a given username with: Is there a similar function to retrieve the username? Answer You are expected to have stored the username somewhe…

python regex get first part of an email address

I am quite new to python and regex and I was wondering how to extract the first part of an email address upto the domain name. So for example if: I would like the regex result to be (taking into account all “sorts” of email ids i.e including numbers etc..): I get the idea of regex – as in I

Using python’s eval() vs. ast.literal_eval()

I have a situation with some code where eval() came up as a possible solution. Now I have never had to use eval() before but, I have come across plenty of information about the potential danger it can cause. That said, I’m very wary about using it. My situation is that I have input being given by a user…

Flask 302 error code when using flask.redirect

I have this code https://github.com/italomaia/flask-empty/blob/master/src/0.8/main.py and I wrote at the end of the file: If I run this code, I get 302 server error (ERR_TOO_MANY_REDIRECTS), but if I change this line return redirect(url_for(‘login’)) by return ‘Hello!’ it works without…

Using cat command in Python for printing

In the Linux kernel, I can send a file to the printer using the following command From what I understand, this redirects the contents in file.txt into the printing location. I tried using the following command I thought this command would achieve the same thing, but it gave me a “Permission Denied&#8221…