Questions tagged [python-2.7]
Python 2.7 is the last major version in the 2.x series, and is no longer maintained since January 1st 2020. Use the generic [python] tag on all Python questions. Do not add this tag simply to convey the version of Python you're using, unless the question concerns an issue specific to Python 2.7.
python-2.7
95,157
questions
633
votes
12
answers
432k
views
Get exception description and stack trace which caused an exception, all as a string
How to convert a caught Exception (its description and stack trace) into a str for external use?
try:
method_that_can_raise_an_exception(params)
except Exception as e:
print(...
560
votes
30
answers
831k
views
How do I create test and train samples from one dataframe with pandas?
I have a fairly large dataset in the form of a dataframe and I was wondering how I would be able to split the dataframe into two random samples (80% and 20%) for training and testing.
Thanks!
560
votes
6
answers
992k
views
Converting dictionary to JSON
r = {'is_claimed': 'True', 'rating': 3.5}
r = json.dumps(r)
file.write(str(r['rating']))
I am not able to access my data in the JSON. What am I doing wrong?
TypeError: string indices must be ...
519
votes
20
answers
1.3m
views
How to fix: "UnicodeDecodeError: 'ascii' codec can't decode byte"
as3:~/ngokevin-site# nano content/blog/20140114_test-chinese.mkd
as3:~/ngokevin-site# wok
Traceback (most recent call last):
File "/usr/local/bin/wok", line 4, in
Engine()
File "...
514
votes
6
answers
592k
views
How to sort a pandas dataFrame by two or more columns?
Suppose I have a dataframe with columns a, b and c. I want to sort the dataframe by column b in ascending order, and by column c in descending order. How do I do this?
487
votes
14
answers
1.1m
views
How do I install the yaml package for Python?
I have a Python program that uses YAML. I attempted to install it on a new server using pip install yaml and it returns the following:
$ sudo pip install yaml
Downloading/unpacking yaml
Could not ...
473
votes
48
answers
1.3m
views
urllib and "SSL: CERTIFICATE_VERIFY_FAILED" Error
I am getting the following error:
Exception in thread Thread-3:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line ...
460
votes
3
answers
300k
views
Delete a dictionary item if the key exists [duplicate]
Is there any other way to delete an item in a dictionary only if the given key exists, other than:
if key in mydict:
del mydict[key]
The scenario is that I'm given a collection of keys to be ...
460
votes
10
answers
609k
views
How to get current time in python and break up into year, month, day, hour, minute?
I would like to get the current time in Python and assign them into variables like year, month, day, hour, minute. How can this be done in Python 2.7?
452
votes
17
answers
1.1m
views
How to uninstall Python 2.7 on a Mac OS X 10.6.4?
I want to completely remove Python 2.7 from my Mac OS X 10.6.4. I managed to remove the entry from the PATH variable by reverting my .bash_profile. But I also want to remove all directories, files, ...
447
votes
6
answers
485k
views
How can I selectively escape percent (%) in Python strings?
I have the following code
test = "have it break."
selectiveEscape = "Print percent % in sentence and not %s" % test
print(selectiveEscape)
I would like to get the output:
Print percent % in ...
441
votes
22
answers
776k
views
bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library?
...
soup = BeautifulSoup(html, "lxml")
File "/Library/Python/2.7/site-packages/bs4/__init__.py", line 152, in __init__
% ",".join(features))
bs4.FeatureNotFound: Couldn't ...
435
votes
8
answers
474k
views
How to write a Python module/package?
I've been making Python scripts for simple tasks at work and never really bothered packaging them for others to use. Now I have been assigned to make a Python wrapper for a REST API. I have absolutely ...
417
votes
17
answers
329k
views
(13: Permission denied) while connecting to upstream:[nginx]
I am working with configuring Django project with Nginx and Gunicorn.
While I am accessing my port gunicorn mysite.wsgi:application --bind=127.0.0.1:8001 in Nginx server, I am getting the following ...
409
votes
11
answers
602k
views
Getting rid of \n when using .readlines() [duplicate]
I have a .txt file with values in it.
The values are listed like so:
Value1
Value2
Value3
Value4
My goal is to put the values in a list. When I do so, the list looks like this:
['Value1\n', 'Value2\n'...