如何使用python3将值写入Excel?

时间:2019-03-27 04:01:31

标签: excel python-3.x

如何使用python3将值写入excel?

import xlsxwriter
from openpyxl import load_workbook
import re

input_file = "No. ; HP 0136510545","good"

for line in input_file:
if "HP" in line:
    s= line  
    hp = re.findall('\d+', s)
    print(hp)


data = [["hp"],[hp]]

workbook = xlsxwriter.Workbook('Input.xlsx')
workbook.close()

wb = load_workbook("Input.xlsx")
ws = wb.worksheets[0]

for row_data in data:
    ws.append(row_data)

 wb.save("excel_file.xlsx") 


Expected Result:
0136510545

Got Result:
['0136510545']

我遇到错误,因为无法转换为excel,我为什么不这样做,但是当我尝试编写为sheet1.write(0,0,hp)时,它将在我的代码中出现问题吗?请帮助我

1 个答案:

答案 0 :(得分:0)

只需进行如下更改:
data = [["hp"],hp]