OUTPUTS = { 'Accelerator Defective\n': '1\n', 'Aggressive Driving/Road Rage\n': '2\n', 'Alcohol Involvement\n': '3\n', 'Animals Action\n': '4\n', 'Backing Unsafely\n': '5\n', 'Brakes Defective\n': '6\n', 'Cell Phone (hand-held)\n': '7\n', 'Cell Phone (hands-free)\n': '8\n', 'Driver Inattention/Distraction\n': '9\n', 'Driver Inexperience\n': '10\n', 'Drugs (Illegal)\n': '11\n', 'Failure to Keep Right\n': '12\n', 'Failure to Yield Right-of-Way\n': '13\n', 'Fatigued/Drowsy\n': '14\n', 'Fell Asleep\n': '15\n', 'Following Too Closely\n': '16\n', 'Glare\n': '17\n', 'Headlights Defective\n': '18\n', 'Illness\n': '19\n', 'Lane Marking Improper/Inadequate\n': '20\n', 'Lost Consciousness\n': '21\n', 'Obstruction/Debris\n': '22\n', 'Other Electronic Device\n': '23\n', 'Other Lighting Defects\n': '24\n', 'Other Vehicular\n': '25\n', 'Outside Car Distraction\n': '26\n', 'Oversized Vehicle\n': '27\n', 'Passenger Distraction\n': '28\n', 'Passing or Lane Usage Improper\n': '29\n', 'Pavement Defective\n': '30\n', 'Pavement Slippery\n': '31\n', 'Pedestrian/Bicyclist/Other Pedestrian Error/Confusion\n': '32\n', 'Physical Disability\n': '33\n', 'Prescription Medication\n': '34\n', 'Reaction to Other Uninvolved Vehicle\n': '35\n', 'Shoulders Defective/Improper\n': '36\n', 'Steering Failure\n': '37\n', 'Tire Failure/Inadequate\n': '38\n', 'Tow Hitch Defective\n': '39\n', 'Traffic Control Device Improper/Non-Working\n': '40\n', 'Traffic Control Disregarded\n': '41\n', 'Turning Improperly\n': '42\n', 'Unsafe Lane Changing\n': '43\n', 'Unsafe Speed\n': '44\n', 'Unspecified\n': '45\n', 'View Obstructed/Limited\n': '46\n', } f = open('file.txt','r') with open('output','a') as o: for line in f: o.write(OUTPUTS.get(line, '47\n'))