我有两列数据,x和y值,需要将文件保存为excel文件,以便在excel中打开。是否有任何模块可以帮助我解决这个问题?
格式必须是xls
数据如下:
4.20985 17.1047
4.82755 16.4046
3.17238 12.1246
4.50796 18.0955
6.04241 21.1016
4.62863 16.4974
4.32245 14.6536
6.48382 19.7664
5.66514 20.1288
6.11072 22.6859
5.55167 15.7504
答案 0 :(得分:2)
您似乎很擅长创建csv
文件,但由于您已经询问了xls
,这里有一个使用{{3}的示例模块:
import xlwt
data = """
4.20985 17.1047
4.82755 16.4046
3.17238 12.1246
4.50796 18.0955
6.04241 21.1016
4.62863 16.4974
4.32245 14.6536
6.48382 19.7664
5.66514 20.1288
6.11072 22.6859
5.55167 15.7504
"""
# prepare two-dimensional list
data = [map(float, item.split()) for item in data.split('\n') if item]
# create workbook and add sheet
workbook = xlwt.Workbook()
sheet = workbook.add_sheet('Test')
# loop over two-dimensional list and write data
for index, (value1, value2) in enumerate(data):
sheet.write(index, 0, value1)
sheet.write(index, 1, value2)
# save a workbook
workbook.save('test.xls')
希望有所帮助。