What is the read_clipboard() function in Python?

The read_clipboard() pandas function reads the content from the clipboard buffer and passes it to the read_csv() function. Basically, it returns a parsed DataFrame instance.

Syntax


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

Parameters

  • sep: It represents the string or regex delimiter. The default 's+' shows one or more whitespace characters.
  • **kwargs: It passes the keyworded argument list of a variable length.

Return value

It returns the parsed DataFrame instance.

Code

The code below explains the read_clipboard() function in detail:

import numpy as np
import pandas as pd
import tkinter as tk
root = tk.Tk()
# copy to clipboard
df = pd.read_csv('Housing.csv')
# Copy data to system buffer
df.to_clipboard(excel = True)
data = pd.read_clipboard() # reading the data from your clipboard
# show data in GUI
label = tk.Label(root,text=data)
label.pack()
root.mainloop()
Demo Code

Code explanation

  • Line 6: We call the pd.read_csv() function to load the House.csv file in the program.
  • Line 8: We invoke the df.to_clipboard() function to copy data to the system clipboard buffer.
  • Line 9: We use pandas’ read_clipboard() function to the read system clipboard buffer.
  • Line 11: We print the Housing.csv file data on the console.