Python——使用xlrd获取列标题,并使用循环创建具有列标题的变量

Python——使用xlrd获取列标题,并使用循环创建具有列标题的变量,python,xlrd,Python,Xlrd,假设我有一个Excel文件,其中包含测试ID(字符串和数字——我自己不命名它们…)和测试期间不同增量的结果(测试在特定增量下进行): 我想用xlrd打开文件。然后,我想创建一个循环,它使用测试的名称创建一个列表。例如: testID_SV-101 = ([300, 363, 461, ..., 421]) testID_GB-B1B = ([226, 344, 444, ..., 444]) testID_67.5 = ([547, 600, 615, ..., 625]) 我的最终目标是使用

假设我有一个Excel文件,其中包含测试ID(字符串和数字——我自己不命名它们…)和测试期间不同增量的结果(测试在特定增量下进行):

我想用xlrd打开文件。然后,我想创建一个循环,它使用测试的名称创建一个列表。例如:

testID_SV-101 = ([300, 363, 461, ..., 421])
testID_GB-B1B = ([226, 344, 444, ..., 444])
testID_67.5 = ([547, 600, 615, ..., 625])
我的最终目标是使用测试结果,计算(比如)从增量4到9的斜率,然后绘制结果。说到这里,这是实现我最终目标的最好方式吗


我已经想出了(在很多帮助下)如何使用gnuplot实现这一点,但我预见它将无法满足我所需要的一切,所以我决定开始学习Python。

您的数据适合Python的数字库
numpy
(但是您也可以使用
Pandas
,但这无论如何都是基于
numpy
)。对于绘图,您应该查看例如

您想要的是将工作表中的每一行存储为结构化数组中的记录。这是因为每一行都是与同一事物(例如测量结果)相关的事物的逻辑组。稍后,您可以通过第一行中给出的名称访问所有记录,这些名称用于标识每个记录的含义。下面是可以工作的代码

>>> import xlrd
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> 
>>> wb = xlrd.open_workbook('sto.xls')
>>> sheet = wb.sheet_by_name('Sheet1')
>>> headers = [str(cell.value) for cell in sheet.row(0)]  # extra conversion to string required for later use in record array header
>>> headers
['Inc.', 'SV-101', 'GB-B1A', '67.5']
>>> arr = []
>>> for rowind in range(sheet.nrows)[1:]:
...     arr.append([ cell.value for cell in sheet.row(rowind)])
... 
>>> arr
[[1.0, 300.0, 226.0, 547.0], [2.0, 363.0, 344.0, 600.0], [3.0, 461.0, 444.0, 615.0]]
>>> data = np.rec.fromrecords(arr, names=headers)
>>> data
rec.array([(1.0, 300.0, 226.0, 547.0), (2.0, 363.0, 344.0, 600.0),
       (3.0, 461.0, 444.0, 615.0)], 
      dtype=[('Inc.', '<f8'), ('SV-101', '<f8'), ('GB-B1A', '<f8'), ('67.5', '<f8')])
>>> data['SV-101']  # You can access columns by their name
array([ 300.,  363.,  461.])
>>> data['67.5']  # bit of a strange header name
array([ 547.,  600.,  615.])
>>> # If you want to plot e.g. the 3rd column vs the 2nd, do this:
>>> plt.plot(data['SV-101'], data['GB-B1A'])
[<matplotlib.lines.Line2D object at 0x2af3c90>]
>>> plt.show()
或者,您可能只需要这两个点之间的切向斜率,在这种情况下,您可以获取这些点上感兴趣的值(例如“GB-B1A”),然后除以相应独立变量(例如“SV-101”)的差值:


顺便说一句,python变量的名称不能是
testID_SV-101
(因为破折号不是)或
testID_67.5
(因为点有不同的含义)。

TypeError:“Row”对象不可iterable
@Cerin:这不是一个非常有用的错误描述(它发生在哪一行),因为xlrd(第页,是,第行)。您是否在使用xlrd?如果您有问题,请提出问题,而不要在评论中对您的问题进行否决和不完整的描述。@Cerin任何反馈?如果您可以指定错误发生的位置,以便其他人不必认为上面的行有任何问题,这将有助于您在许多b行中找到日志和教程。我在一张由
xlutils.copy.copy()
复制的工作表上操作。显然,尽管名称不同,但它不会生成相同类类型的实例,因此在普通工作表实例中缺少大多数常用属性和迭代器。
>>> import xlrd
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> 
>>> wb = xlrd.open_workbook('sto.xls')
>>> sheet = wb.sheet_by_name('Sheet1')
>>> headers = [str(cell.value) for cell in sheet.row(0)]  # extra conversion to string required for later use in record array header
>>> headers
['Inc.', 'SV-101', 'GB-B1A', '67.5']
>>> arr = []
>>> for rowind in range(sheet.nrows)[1:]:
...     arr.append([ cell.value for cell in sheet.row(rowind)])
... 
>>> arr
[[1.0, 300.0, 226.0, 547.0], [2.0, 363.0, 344.0, 600.0], [3.0, 461.0, 444.0, 615.0]]
>>> data = np.rec.fromrecords(arr, names=headers)
>>> data
rec.array([(1.0, 300.0, 226.0, 547.0), (2.0, 363.0, 344.0, 600.0),
       (3.0, 461.0, 444.0, 615.0)], 
      dtype=[('Inc.', '<f8'), ('SV-101', '<f8'), ('GB-B1A', '<f8'), ('67.5', '<f8')])
>>> data['SV-101']  # You can access columns by their name
array([ 300.,  363.,  461.])
>>> data['67.5']  # bit of a strange header name
array([ 547.,  600.,  615.])
>>> # If you want to plot e.g. the 3rd column vs the 2nd, do this:
>>> plt.plot(data['SV-101'], data['GB-B1A'])
[<matplotlib.lines.Line2D object at 0x2af3c90>]
>>> plt.show()
>>> np.diff(data['SV-101'])
array([ 63.,  98.])
>>> (data['GB-B1A'][2] - data['GB-B1A'][0])/(data['SV-101'][2] - data['SV-101'][0])
1.3540372670807452