Seperating the numbers from strings to do the maths and return the string with the results [on hold]

120
November 18, 2019, at 06:40 AM

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

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

For example:

---------------------------
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
---------------------------

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: ')))
Answer 1

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.

Splitting Receipts

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

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']

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

Parsing Line Items

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'

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.

Compose

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

Rent Charter Buses Company
READ ALSO
Anaconda Python Jupyter Notebook is broken on Catalina 10.15.1

Anaconda Python Jupyter Notebook is broken on Catalina 10.15.1

Attempting to run first code cell of jupyter notebook

153
How do I find distance between two geographical coordinates using OpenStreetMap's api?

How do I find distance between two geographical coordinates using OpenStreetMap's api?

I would like to make automated calls in python to get the data that the question asks forA script that accomplishes this task would be helpful

99
How to print second to last item in enumeration?

How to print second to last item in enumeration?

I am printing the last item in a list that I enumerateHow can I also print the second to last item?

108
Setting hyperparameter optimization bounds in GPflow 2.0

Setting hyperparameter optimization bounds in GPflow 2.0

In GPflow 10, if I wanted to set hard bounds on a parameter like lengthscale (i

90