Skip to main content

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
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(...
bluish's user avatar
  • 27k
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!
tooty44's user avatar
  • 7,049
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 ...
sheetal_158's user avatar
  • 7,791
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 "...
fisherman's user avatar
  • 10.8k
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?
Rakesh Adhikesavan's user avatar
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 ...
harperville's user avatar
  • 7,279
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 ...
user3724476's user avatar
  • 5,070
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 ...
Simon Hughes's user avatar
  • 4,999
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?
guagay_wk's user avatar
  • 27.5k
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, ...
Jan Deinhard's user avatar
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 ...
jondykeman's user avatar
  • 6,242
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 ...
user3773048's user avatar
  • 6,119
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 ...
yowmamasita's user avatar
  • 4,900
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 ...
Mulagala's user avatar
  • 8,491
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'...
TDNS's user avatar
  • 4,325

15 30 50 per page
1
2 3 4 5
6344