Python Input/Output – The Clipboard

Over your career as a Data Scientist, there may be instances where you will work with data to/from the system Clipboard.  This article shows you how to manipulate this data.

Install Required Library

Before any data manipulation can occur, a new library will require installation. The pandas library enables access to/from a DataFrame.

To install this library, navigate to an IDE terminal. At the command prompt ($), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($). Your terminal prompt may be different.

$ pip install pandas

Hit the <Enter> key on the keyboard to start the installation process.

If the installations was successful, a message displays in the terminal indicating the same.

Read Data from Clipboard

Function Outline

pandas.read_clipboard(sep='\\s+', **kwargs)

returns a parsed DataFrame object.

This function reads text from the system Clipboard and passes this data to the read_csv() function. Pass in the appropriate parameter(s), save, and you have a new CSV containing the Clipboard data!

Example 1

Say you are the Data Scientist for Arc Recordings. Your boss has found a list of the ten most promising songwriters for the year on a website. He wants you to copy this data and forward it to him in a CSV file.

The first step is to copy the contents of the rows and columns below by highlighting the text, then press CTL+C to copy the contents to the system Clipboard.

Copy to Clipboard

FnameLnameSongsManagers-NameManagers-Phone
JoshWasher23Bob Jones1-809-333-1212
MichaelHudson13Steve Smith1-821-321-5444
RussCooper34Cary Waters1-302-567-5465
AnnaCzecka18Howie Brown1-523-777-8790
LukeAncaster27James Kirker1-555-412-5657
ShaneKirk72Sarah Waters1-312-666-7879
MorganFreeds21Giselle Henri1-812-553-6262
ErnieWalker43Tony Hamilton1-902-555-1212

Once that is done, run the code below.

import pandas as pd
df = pd.read_clipboard()
print(df)
  • Line [2] copies the contents of the system Clipboard to the DataFrame (df). Note: by default, the field separator character is whitespace.
  • Line [3] outputs the contents to the terminal.

Output

The output should be the same as shown above.

Example 2

For this example, we have a list of the name and phone numbers of the Managers from above. The field separator is a comma (,).

The first step is to copy the contents of the rows and columns below by highlighting the text, then press CTL+C to copy the contents to the system Clipboard.

Copy to Clipboard

Mgr-Name, Mgr-Phone
Bob Jones,1-809-333-1212
Steve Smith,1-821-321-5444
Cary Waters,1-302-567-5465
Howie Brown,1-523-777-8790
James Kirker,1-555-412-5657
Sarah Waters,1-312-666-7879
Giselle Henri,1-812-553-6262
Tony Hamilton,1-902-555-1212

Once that is done, run the code below. Note that since we are not parsing on whitespace (default), the separator parameter must exist.

import pandas as pd
df = pd.read_clipboard(',')
print(df)
  • Line [2] copies the contents of the system Clipboard to the DataFrame (df). The separator this time is a comma (,). Since the comma is not the default, it must exist.
  • Line [3] outputs the contents to the terminal.

Output:

The output will be similar to the following:

 Mgr-NameMgr-Phone
0Bob Jones1-809-333-1212
1Steve Smith1-821-321-5444
2Cary Waters1-302-567-5465
3Howie Brown1-523-777-8790
4James Kirker1-555-412-5657
5Sarah Waters1-312-666-7879
6Giselle Henri1-812-553-6262
7Tony Hamilton1-902-555-1212

A few additional notes about the Clipboard:

  • For all available parameters, click here.
  • As seen above, whitespace is the default separator character unless otherwise stipulated.

DataFrame to Clipboard

Function Outline

DataFrame.to_clipboard(excel=True, sep=None, **kwargs)

copies a DataFrame to the Clipboard.

The first step is to copy the contents of the rows and columns below by highlighting the text, then press CTL+C to copy the contents to the system Clipboard.

Copy to Clipboard

Mgr-Name, Mgr-Phone
Bob Jones,1-809-333-1212
Steve Smith,1-821-321-5444
Cary Waters,1-302-567-5465
Howie Brown,1-523-777-8790
James Kirker,1-555-412-5657
Sarah Waters,1-312-666-7879
Giselle Henri,1-812-553-6262
Tony Hamilton,1-902-555-1212

Once that is done, run the code below. Note that since we are not parsing on whitespace (default), the separator parameter must exist.

import pandas as pd
df.to_clipboard(sep=',') 
print(df)
  • Line [2] reads the contents of the system Clipboard to the DataFrame (df). The separator this time is a comma (,). Since the comma is not the default, it must exist.
  • Line [3] outputs the contents to the terminal.

Output

 Mgr-NameMgr-Phone
0Bob Jones1-809-333-1212
1Steve Smith1-821-321-5444
2Cary Waters1-302-567-5465
3Howie Brown1-523-777-8790
4James Kirker1-555-412-5657
5Sarah Waters1-312-666-7879
6Giselle Henri1-812-553-6262
7Tony Hamilton1-902-555-1212

Save to CSV

The last step is to save the system Clipboard contents from Section 3 above. If you don`t already have this in your system Clipboard, do this before proceeding.

Run the following code to save the contents of the system Clipboard to the managers.csv file.

import pandas as pd
df = pd.read_clipboard(',')
df.to_clipboard(sep=',') 
df.to_csv('managers.csv', encoding='utf-8')
  • Line [2] reads the contents of the system clipboard to the DataFrame (df). The separator this time is a comma (,). Since the comma is not the default, it must exist.
  • Line [3] sends the contents of the DataFrame to the system Clipboard.
  • Line [4] saves the contents to the managers.csv file. Setting the encoding parameter catches and prevents any UnicodeEncodeError from occurring.

Open the CSV created and view the contents.

Output:

This article uses a small amount of data. However, the functions mentioned above are real-time-savers to produce results in a short amount of time.