XLRD 拆分单元格的逗号分隔值
XLRD splitting comma separated values of a cell
我正在使用 XLRD 库读取 excel sheet 的特定单元格。此单元格可能有也可能没有多个逗号分隔值。
请在下面找到我的代码片段
from xlrd import open_workbook
from argparse import ArgumentParser as parser
parse = parser()
parse.add_argument('-p', '--path', action='store', dest='path', help='mention the excel sheet path')
args = parse.parse_args()
excelList = []
book = open_workbook(args.path)
dep_cms = book.sheet_by_index(1)
for row_index in range(1, dep_cms.nrows):
excelList.append(dep_cms.cell(row_index, 8).value)
print(excelList)
for name in excelList:
print('Dependent CMS is {}'.format(name))
打印输出是这样的...
['187013, 187014, 187015']
Dependent CMS is 187013, 187014, 187015
它将逗号分隔值视为单个字符串。
理想的输出必须如下所示
['187013', '187014', '187015']
Dependent CMS is 187013
Dependent CMS is 187014
Dependent CMS is 187015
如何拆分成CS值?我知道我们可以使用 split 来拆分字符串。但是在这种情况下如何使用它呢?
此致
PS:
根据评论部分的建议,我尝试拆分
结果是这样的
[['187013', ' 187014']]
Dependent CMS is ['187013', ' 187014']
如果我在split中使用space而不是逗号作为分隔符,结果如下
[['187013,', '187014']]
Thy name is ['187013,', '187014']
它正在一个数组中创建一个数组,这不是我想要的。
根据答案的建议,以下代码段有效。
excelList.extend([x.strip() for x in dep_cms.cell(row_index, 8).value.split(',')])
也许这段代码会有所帮助。它的工作原理是用逗号分隔名称并将新列表放入数组中。
name = ['187013, 187014, 187015']
names = name[0].split(",") # This separates the first el of name on the comma
for id in names:
print ("Dependent CMS is " + id.strip())
# prints:
Dependent CMS is 187013
Dependent CMS is 187014
Dependent CMS is 187015
尝试:
excelList = []
for row_index in range(1, dep_cms.nrows):
excelList.extend([x.strip() for x in dep_cms.cell(row_index, 8).value.split(',')])
我正在使用 XLRD 库读取 excel sheet 的特定单元格。此单元格可能有也可能没有多个逗号分隔值。
请在下面找到我的代码片段
from xlrd import open_workbook
from argparse import ArgumentParser as parser
parse = parser()
parse.add_argument('-p', '--path', action='store', dest='path', help='mention the excel sheet path')
args = parse.parse_args()
excelList = []
book = open_workbook(args.path)
dep_cms = book.sheet_by_index(1)
for row_index in range(1, dep_cms.nrows):
excelList.append(dep_cms.cell(row_index, 8).value)
print(excelList)
for name in excelList:
print('Dependent CMS is {}'.format(name))
打印输出是这样的...
['187013, 187014, 187015']
Dependent CMS is 187013, 187014, 187015
它将逗号分隔值视为单个字符串。
理想的输出必须如下所示
['187013', '187014', '187015']
Dependent CMS is 187013
Dependent CMS is 187014
Dependent CMS is 187015
如何拆分成CS值?我知道我们可以使用 split 来拆分字符串。但是在这种情况下如何使用它呢?
此致
PS:
根据评论部分的建议,我尝试拆分
结果是这样的
[['187013', ' 187014']]
Dependent CMS is ['187013', ' 187014']
如果我在split中使用space而不是逗号作为分隔符,结果如下
[['187013,', '187014']]
Thy name is ['187013,', '187014']
它正在一个数组中创建一个数组,这不是我想要的。
根据答案的建议,以下代码段有效。
excelList.extend([x.strip() for x in dep_cms.cell(row_index, 8).value.split(',')])
也许这段代码会有所帮助。它的工作原理是用逗号分隔名称并将新列表放入数组中。
name = ['187013, 187014, 187015']
names = name[0].split(",") # This separates the first el of name on the comma
for id in names:
print ("Dependent CMS is " + id.strip())
# prints:
Dependent CMS is 187013
Dependent CMS is 187014
Dependent CMS is 187015
尝试:
excelList = []
for row_index in range(1, dep_cms.nrows):
excelList.extend([x.strip() for x in dep_cms.cell(row_index, 8).value.split(',')])