Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
188 views
in Technique[技术] by (71.8m points)

python - So I have this text file that I am working with, and I want to use the info in it for my parking system

A line from my text file:

RCF585 medium Joseph -you entered the parking lot at 3:30 and left at 4:30- 

What I want to do is identify the car by the license number, and then know that it is "medium" (the size of it) and therefore the total cost for it is (parking cost * 1.25 (tax)) meanwhile the total for small is (parking cost * 1.00) and big (parking cost * 1.50).

The parking cost (20 usd per half an hour) depends of course on how much time the car have been parked so my second problem is how to read and identify how much a car has parked by reading the line of the relevant car. Here is what I have succeeded to write till now:

f=open(file, "r")

which_car= input("Please write your license number: ")

for line in f:

if line.startswith(which_car):        #identifying which car we want to deal with
question from:https://stackoverflow.com/questions/65599624/so-i-have-this-text-file-that-i-am-working-with-and-i-want-to-use-the-info-in-i

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I would use Regex to find the times in the text,

import re

regex = re.compile(r'(d{1,2}:dd)')
times = regex.findall(line)

and use datetime and dateutil.relativedelta to find the parking time,

from datetime import datetime
from dateutil.relativedelta import relativedelta

parking_time = relativedelta(datetime.strptime(times[0] , '%H:%M'), datetime.strptime(times[1] , '%H:%M'))
minutes = abs((parking_time.hours * 60) + parking_time.minutes)

So, all together would be:

import re
from datetime import datetime
from dateutil.relativedelta import relativedelta

which_car = input("Please write your license number: ")

with open(file, 'r') as f:
    
    for line in f:

        if line.startswith(which_car):

            size = line.split(' ')[1]

            regex = re.compile(r'(d{1,2}:dd)')
            times = regex.findall(line)
             
            # Update - Ex. 3:27 -> 3:30, 3:35 -> 3:30
            for i in range(len(times)):

                minute = int(times[i].split(':')[1])

                if minute - ((minute // 15) * 15) < 7.5:
                    minute = (minute // 15) * 15
                else:
                    minute = (minute // 15 + 1) * 15

                times[i] = times[i].split(':')[0] + ':' + str(minute)

            parking_time = relativedelta(datetime.strptime(times[0] , '%H:%M'), datetime.strptime(times[1] , '%H:%M'))

            minutes = abs((parking_time.hours * 60) + parking_time.minutes)

            parking_cost = (minutes // 30) * 20
            total_cost = parking_cost   # Default size is small

            if size == 'big':
                total_cost = parking_cost * 1.5
            elif size == 'medium':
                total_cost = parking_cost * 1.25

I suggest using with when working with files since you don't have to worry about closing the file manually anymore.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...