Python文件处理:针对特定数字进行搜索

时间:2015-03-02 10:32:29

标签: python

我正在制作一份文件,其中我需要记录车辆牌照(这是一次练习,没有任何违法行为),并计算他们行驶的速度并显示所有超过60岁的车辆英里每小时。我对文件处理很陌生,不知道如何让Python搜索特定的数字。救命啊!

到目前为止

代码:

plate = "blank"
sensor1 = "blank"
sensor2 = "blank"
timetaken = "blank"
speed = "blank"

plate = input("Please input the license plate of the vehicle.")

sensor1 = float(input("Input the time the vehicle passed sensor 1."))
sensor2 = float(input("Input the time the vehicle passed sensor 2."))

timetaken = (sensor2 - sensor1)

print("The time taken for the vehicle to travel between the two sensors in  seconds:")
print(timetaken)

speed = (5/timetaken)
print("Vehicle speed in miles per hour:")
print(speed)

编辑:我很感激帮助,但我不确定如何搜索稍后将要创建的.txt文档以搜索特定的牌照。

2 个答案:

答案 0 :(得分:0)

if (speed>60)
    #Do what you want to 

答案 1 :(得分:0)

在这部分中,您将速度存储在变量中,然后在第二行中用字符串覆盖它。

speed = (5/timetaken)
speed = ("Vehicle speed in miles per hour:")
print(speed)

正如Mohit Bhasi提到的检查速度并不困难,只要确保你知道你使用的是哪个单位,无论是每秒米还是每秒公里数。

在Python和documentation offers few examples how to do it中读/写文件相当简单。 Or check out some other sources as well

关于你的任务,我会做这样的事情:

  1. 打开文件
  2. 用数据读取每一行
  3. 获取板块字符串,并将速度值解析为数字
  4. 检查速度限制
  5. 打印
  6. 关闭文件
  7. PS:没有人会为你写的。这将带来一切乐趣。 : - )