python - 我怎样才能摆脱 `' '` and ` ()` 字符?

标签 python python-3.x

我正在执行这段代码:

def filewrite():
    filew = open('company.dat', 'a')
    custid = str(input('Enter Customer ID:'))
    Widg = abs(float(input('Quantity of Widgets Ordered:')))
    Gidg = abs(float(input('Quantity of Gidgets Ordered:')))
    Dood = abs(float(input('Quantity of Doodads Ordered:')))
    widt = Widg * 10.55
    gidt = Gidg * 7.30
    doodt = Dood * 5.25
    total = widt + gidt + doodt
    wida = (Widg, 'Widgets Ordered @ $10.55', format(widt,',.2f'))
    gida = (Gidg, 'Gidgets Ordered @ $7.30', format(gidt,',.2f'))
    dooda = (Dood, 'Doodads Ordered @ $5.25', format(doodt,',.2f'))
    filew.write('\n')
    filew.write(custid + '\n')
    filew.write(str(wida))
    filew.write(str(gida))
    filew.write(str(dooda))
    filew.close()


def fileread():
    filer = open('company.dat', 'r')
    read = filer.read()
    filer.close()
    print(read, end='')

again = True

while again == True:
    print('1 - Add a new Invoice Order')
    print('2 - Display all Invoice Orders')
    print('3 - Quit')

    choice = abs(int(input('Enter 1, 2, or 3:')))

    if choice > 3:
        print('Error, no choice of such available.')
        choice = abs(int(input('Enter 1, 2, or 3:')))


    if choice == 1:
        filewrite()
        print('\n')

    if choice == 2:
        fileread()
        print('\n')

    if choice == 3:
        exit()

当我输入所有内容并读取文件时,结果如下:

As234
(12.0, 'Widgets Ordered @ $10.55', '126.60')(1.0, 'Gidgets Ordered @ $7.30', 
'7.30')(3.0, 'Doodads Ordered @ $5.25', '15.75')

我怎样才能摆脱显示的 ()''

我是编码新手,刚开始学习 Python 类(class),这是我的作业之一,我一直坚持这一点。

最佳答案

您的问题是如何创建变量。 在这里,您将一些变量创建为整数:

Widg = abs(float(input('Quantity of Widgets Ordered:')))
Gidg = abs(float(input('Quantity of Gidgets Ordered:')))
Dood = abs(float(input('Quantity of Doodads Ordered:')))

然后在这里您使用每个变量创建一个列表:

wida = (Widg, 'Widgets Ordered @ $10.55', format(widt,',.2f'))
gida = (Gidg, 'Gidgets Ordered @ $7.30', format(gidt,',.2f'))
dooda = (Dood, 'Doodads Ordered @ $5.25', format(doodt,',.2f'))

然后,当您将其转换为字符串 str(wida) 时,您将准确获得打印的内容,因为这是您为转换提供的内容。

您应该检查要创建的变量类型,然后执行以下操作之一:

  1. 通过格式化列表中的项目来打印
  2. 创建一个类来保存您当前在列表(wida、gida、dooda)中的项目,您可以在其中单独获取类中的每个变量。

如果您对第二个选项有任何疑问,请发表评论或阅读 documentation .

关于python - 我怎样才能摆脱 `' '` and ` ()` 字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53630429/

相关文章:

python - Pandas :在另一列中创建包含 'next' 更改值的列

python - 如何将加速度加到速度上?

python - 在 Python 中从文件中读取数据

python - 可以在 HTML 中使用 Python 和 Jinja2 刷新网页吗?

python - 创建 TF-IDF 矩阵 Python 3.6

python - 如何检查字符串是否表示 float

python - 如何不打印命令输出?

python - “classmethod”对象在 Python 3 的元类方法中不可调用

Python pexpect 正则表达式匹配包含在 b' ' 中

python - PyQt5 GUI 退出时为 "Python has stopped working"