Copy markdown formatted tables from SO to pandas clipboard

Viewed 261

I am comfortable in using pd.read_clipboard() when the data posted on SO has spaces between columns ('\s\s+')

However, how would one copy the below table directly to a pandas dataframe?

Record ID Shared On
(UNIX timestamp)
Share type Share To User
1 1611872850 shared user A
2 1611872851 shared user B
3 1611872852 shared user B
1 1611872853 share_removed user A

I tried finding something that would help me with this and the closest I got to copying this dataframe was using the following which has a lot of unnecessary columns and unnecessary spaces that I have to later use df.col.str.strip() to clean up.

#Clicking edit on the question, and copying the underlying markdown
pd.read_clipboard('|')
   Unnamed: 0   Record ID    Shared On<br/>(UNIX timestamp)    \
0         NaN  ------------                      ------------   
1         NaN   1                                 1611872850    
2         NaN   2                                 1611872851    
3         NaN   3                                 1611872852    
4         NaN   1                                 1611872853    

    Share type       Share To User   Unnamed: 5  
0  ---------------  ---------------         NaN  
1   shared           user A                 NaN  
2   shared           user B                 NaN  
3   shared           user B                 NaN  
4   share_removed    user A                 NaN  

Anyone know of a better way? Thanks!

1 Answers

Looks like

pd.read_clipboard(sep='\s*\|\s*').iloc[1:,1:-1]

works pretty well. Output:

  Record ID Shared On<br/>(UNIX timestamp)     Share type Share To User
1         1                     1611872850         shared        user A
2         2                     1611872851         shared        user B
3         3                     1611872852         shared        user B
4         1                     1611872853  share_removed        user A
Related