将数字与字符串分开以进行数学运算并返回带有结果的字符串 [英] Seperating the numbers from strings to do the maths and return the string with the results

查看:104
本文介绍了将数字与字符串分开以进行数学运算并返回带有结果的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些这样的文件.它们是饭店的一些收据.

I have some files like this. They are some receipts of restaurants.

---------------------------
CompanyID: 000000000000
ProductName: quantity costPerPizza sumCost
---------------------------

例如:

---------------------------
CompanyID: 000000000000
Pizza: 2   3.15    6.30
spaghetti:  1   7    7
ribye: 2  40  80
---------------------------
CompanyID: 000000000001
burger: 1   3.15    6.30
spaghetti:  1   7    7
ribye: 2  40  80
---------------------------

如果用户输入了产品名称,我想打印CompanyID以及销售量.

If the user gives input of a product name, I want to print the CompanyID and how many sales.

f=list(open(input('\nGive me the Filename: ')))

推荐答案

此问题有几个不同的组件.首先,如何将收据分成各个公司.接下来,您需要能够解析公司的ID.最后,您需要能够分析订单项的数量,成本和总成本.

There are a few different components to this problem. First, how do you split the receipt into each individual company. Next you need to be able to parse the company's ID. Finally you need to be able to parse the quantity, cost, and total cost from a line item.

您拆分收据的方法取决于分隔符.如果知道连字符的数量相同,则可以在其上拆分输入.

Your method of splitting receipts depends on the separator. If you know that the number of hyphens is the same, you can split your input on that.

SEPARATOR = "---------------------------"

with open('input-file.txt') as f:
    contents = f.read()

# We only want to keep chunks of the file that have content.
company_receipts = []
for receipt in contents.split(SEPARATOR):
    if receipt.trim():
        company_receipts.append(receipt)

这应该给您一个列表,其中每个项目都类似于:

This should give you a list where each item is something like:

CompanyID: 000000000000
Pizza: 2   3.15    6.30
spaghetti:  1   7    7
ribye: 2  40  80

解析公司ID

这里的简单假设是,每张收据的第一行是公司.我们可以使用以前使用的相同的split操作来分离收据的行以及行的各个部分.

Parsing Company IDs

The simple assumption here would be that the first line of every receipt is the company. We can use the same split operation we used before to separate the lines of the receipt as well as the parts of the line.

text = \
"""
CompanyID: 000000000000
Pizza: 2   3.15    6.30
spaghetti:  1   7    7
ribye: 2  40  80
"""

lines = text.split('\n')
company_line = lines[0]

print(company_line.split(' '))
# Output: ['CompanyID:', '000000000000']

您可以使用它们的索引提取该行的相关部分,并可以使用int(value)将字符串转换为数字.

You can pull out the relevant parts of the line using their indices and you can convert strings to numbers with int(value).

您还可以使用拆分从每个订单项获取数量,成本和总成本.从上方使用lines变量:

You can also use splits to get quantities, costs, and total costs from each line item. Using the lines variable from above:

# Remember lines also includes the company line, which we want to exclude
items = lines[1:]

for item in items:
    item_components = item.split(' ')

    # Now you can pull out the components you want
    item_name = item_components[0]         # Example: 'Pizza:'
    item_cost = item_components[1].trim()  # Example: '3.15'

由于这些成分中的一些是小数,因此可以使用float(而不是int)解析它们,尽管对于金钱来说,您可能想用美分而不是美元来处理,所以所有内容都是整数,没有舍入问题.

Since some of these components are decimals, you can parse them with float (instead of int), although with money you may want to deal with cents instead of dollars so everything is whole numbers and there are no rounding issues.

您应该能够使用上面列出的逻辑来构成自己的逻辑并实现您的目标.

You should be able to use the logic laid out above to compose your own logic and accomplish your goal.

这篇关于将数字与字符串分开以进行数学运算并返回带有结果的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆