问题
I often work with excel sheets of which I want to copy just one specific column of numbers into my python script for plotting purposes. This has to be done manually since it is always a different file, columns and rows.
To use numpy arrays, I need the data with a trailing comma to create a python array in this way (I had to add spaces, otherwise stackexchange would post it in a line):
myArray=np.array([
1,
2,
3,
4,
6
)]
So after copying the column with the numbers from excel, I have to add the commas manually. If I have a lot of data, I'll add a column in Excel where I add alle the commas and copy the numbers with the commas together. However I feel like there should be a better way to do this.
Do you guys know a way that python might swallow my data without commas? E.g.
myData = someFunction(
1
2
3
4
6
)
Or do you have another Idea how to add the commas more elegant? I'm using Spyder with Python 3.
Thanks!
回答1:
Edit: If the requirement is to copy one column or row and not a grid. in_
below can be used with replace to produce a comma separated string.
# First copy a column from a spread sheet to the clipboard.
# in_() will return the values separated by newlines '\n'.
# replace these by comma
in_()
# Out[21]: '3\n13\n23\n33\n43\n53\n63\n73\n83\n93\n103\n113\n123\n'
in_().replace('\n', ',')
# Out: '3,13,23,33,43,53,63,73,83,93,103,113,123,'
# If copying a row the copied separator is tab '\t'
# Copy a row in a spreadsheet
in_()
# Out: '60\t61\t62\t63\t64\t65\t66\t67\t68\n'
in_().replace('\t', ',')[:-1] # [-1] removes the newline character.
# Out[25]: '60,61,62,63,64,65,66,67,6'
I wrote the in_()
and from_grid()
to copy data from spreadsheets into variables I wanted to use in my python terminal. They let a user copy a region in a spreadsheet to the clipboard. in_()
will return the clip as a string. from_grid()
will return this converted to a list of lists of strings.
import tkinter as tk
import numpy as np
def in_():
"""
Returns the contents of the clipboard as text.
Usage: txt = in_()
"""
clip=tk.Tk()
clip.withdraw()
temp=clip.selection_get(selection="CLIPBOARD")
clip.destroy()
return temp
def from_grid(delimit_row="\n", delimit_cell="\t"):
"""
Returns a list of lists copied from the clipboard.
Usage: grid=from_grid(delimit_row="\t", delimit_cell="\n")
grid is a list of lists
[ [ r00, r01, r02, ... ],
[ r10, r11, r12, ... ],
....
]
by defaut: the row delimiter is a newline, "\n"
the cell delimiter is a tab character, "\t"
This will paste a copied region of a spreadsheet into a list of lists.
"""
txt=in_()
rows=txt.split(delimit_row)
ret=[]
for row in rows:
temp=row.split(delimit_cell)
ret.append(temp)
return ret[:-1] # A final empty last row is appended.
# This loses it.
import numpy as np
def to_floats( data ):
result= []
for row in data:
temp = []
for item in row:
temp.append(float(item))
result.append(temp)
return np.array(result)
arr = to_floats( from_grid() )
This doesn't do exactly what you've asked for but gives a way of getting the spreadsheet data into python where it can be processed.
Running this in a python console will let the user print the results, which can then be copied into a script.
There may be neater ways to do it. There are some Libraries around, Pyperclip is one but I've never used it.
来源:https://stackoverflow.com/questions/61256050/ideas-for-manual-copy-paste-from-excel-to-python-code-in-spyder