【发布时间】:2020-06-03 07:03:42
【问题描述】:
【问题讨论】:
标签: python excel string pandas xlwings
【问题讨论】:
标签: python excel string pandas xlwings
import xlwings as xw
使用xlwings打开excel文件
filename = r'input.xlsx'
book = xw.Book(filename)
sheet = book.sheets[0]
在本例中从column 'A' 的特定范围内找到sheet 的last row
lrow = sheet.range('A' + str(sheet.cells.last_cell.row)).end('up').row
为您将搜索的string 和您的输出所在的column 声明一个单独的变量。
search_string = '[game]'
sheet.range('B1').value = 'output'
output_index = 2
现在循环遍历该范围以查看您的 search_string 是否在该范围内
for i in range(1, lrow + 1):
if search_string in str(sheet.range('A{}'.format(i)).value):
temp = str(sheet.range('A{}'.format(i)).value)
temp = temp.split(search_string)[1]
if '[' in temp:
temp = temp.split('[')[0]
sheet.range('B{}'.format(output_index)).value = temp
output_index += 1
book.save()
book.close()
下面是完整代码>>
import xlwings as xw
filename = r'input.xlsx'
book = xw.Book(filename)
sheet = book.sheets[0]
lrow = sheet.range('A' + str(sheet.cells.last_cell.row)).end('up').row
search_string = '[game]'
sheet.range('B1').value = 'output'
output_index = 2
for i in range(1, lrow + 1):
if search_string in str(sheet.range('A{}'.format(i)).value):
temp = str(sheet.range('A{}'.format(i)).value)
temp = temp.split(search_string)[1]
if '[' in temp:
temp = temp.split('[')[0]
sheet.range('B{}'.format(output_index)).value = temp
output_index += 1
book.save()
book.close()
【讨论】: