我需要在docker中使用python运行一个简单的算术程序,并在excel中显示输出?
# Python Program to Perform Arithmetic Operations
import xlsxwriter
import xlrd
num1 = float(input(" Please Enter the First Value Number 1: "))
num2 = float(input(" Please Enter the Second Value Number 2: "))
# Add Two Numbers
add = num1 + num2
# Subtracting num2 from num1
sub = num1 - num2
# Multiply num1 with num2
multi = num1 * num2
# Divide num1 by num2
div = num1 / num2
# Modulus of num1 and num2
mod = num1 % num2
# Exponent of num1 and num2
expo = num1 ** num2
print("The Sum of {0} and {1} = {2}".format(num1, num2, add))
print("The Subtraction of {0} from {1} = {2}".format(num2, num1, sub))
print("The Multiplication of {0} and {1} = {2}".format(num1, num2, multi))
print("The Division of {0} and {1} = {2}".format(num1, num2, div))
print("The Modulus of {0} and {1} = {2}".format(num1, num2, mod))
preparedList = [num1,num2,add,sub,multi,div,mod]
loc = ('output/maths.xlsx')
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
itemList = []
for i in range(sheet.nrows):
itemList.append(sheet.row_values(i))
itemList.append(preparedList)
workbook = xlsxwriter.Workbook(loc)
worksheet = workbook.add_worksheet()
row = 0
for items in itemList:
col = 0
for val in items:
worksheet.write(row, col, val)
col = col + 1
row = row + 1
workbook.close()
它正确显示了以下输出,但是我需要将输出存储在Excel工作表中。
PS E:\bala\docker-python-app> docker run -it python-app
Please Enter the First Value Number 1: 45
Please Enter the Second Value Number 2: 5
The Sum of 45.0 and 5.0 = 50.0
The Subtraction of 5.0 from 45.0 = 40.0
The Multiplication of 45.0 and 5.0 = 225.0
The Division of 45.0 and 5.0 = 9.0
The Modulus of 45.0 and 5.0 = 0.0
答案 0 :(得分:0)
尝试使用以下命令运行图像:
docker run -it image
图像将与终端以交互方式启动。
更多有用的信息可以通过下面的链接找到:
答案 1 :(得分:0)
对于交互模式,您需要提及-it
。以交互模式(hence -it flag
)启动容器,该模式允许您与容器的/bin/bash
或/bin/ash
进行交互
FROM python:3.7.4-alpine3.10
RUN echo $'#!/usr/bin/python \n\
num1 = float(input(\" Please Enter the First Value Number 1: \")) \n\
num2 = float(input(\" Please Enter the Second Value Number 2: \")) \n\
# Add Two Numbers \n\
add = num1 + num2 \n\
sub = num1 - num2 \n\
multi = num1 * num2 \n\
# Divide num1 by num2 \n\
div = num1 / num2 \n\
# Modulus of num1 and num2 \n\
mod = num1 % num2 \n\
# Exponent of num1 and num2 \n\
expo = num1 ** num2 \n\
print(\"The Sum of {0} and {1} = {2}\".format(num1, num2, add)) ' >> /root/ab.py
CMD [ "python" , "/root/ab.py"]
现在建立图片
docker build -t pyinput .
运行容器
docker run --rm -it pyinput
更新:
docker run命令
docker run --rm -it -v $PWD/output:/output pyinput ash -c "python /root/ab.py"
挂载目录,这样您将在主机上看到输出。 这是完整的示例,我只是添加一个值,请为自己做好准备
FROM python:3.7.4-alpine3.10
RUN mkdir -p /output
RUN touch /output/maths.xlsx
RUN pip install xlsxwriter xlrd
RUN pip install xlwt
RUN echo $'#!/usr/bin/python \n\
import xlsxwriter \n\
import xlrd \n\
import xlwt \n\
from xlwt import Workbook \n\
num1 = float(input(\" Please Enter the First Value Number 1: \")) \n\
num2 = float(input(\" Please Enter the Second Value Number 2: \")) \n\
# Add Two Numbers \n\
add = num1 + num2 \n\
sub = num1 - num2 \n\
multi = num1 * num2 \n\
# Divide num1 by num2 \n\
div = num1 / num2 \n\
# Modulus of num1 and num2 \n\
mod = num1 % num2 \n\
# Exponent of num1 and num2 \n\
expo = num1 ** num2 \n\
print(\"The Sum of {0} and {1} = {2}\".format(num1, num2, add)) \n\
preparedList = [num1,num2,add,sub,multi,div,mod] \n\
wb = Workbook() \n\
sheet1 = wb.add_sheet(\'Sheet 1\') \n\
sheet1.write(1, 0, \'Add\') \n\
sheet1.write(0, 1, \'Add\') \n\
sheet1.write(1, 1, add) \n\
wb.save(\'/output/xlwt example.xls\') \n\
print ( \"Entries saved to /output/xlwt example.xls\")' >> /root/ab.py
CMD [ "python" , "/root/ab.py"]