[英]How to pick random cars in python from txt file
i've came across this problem that i've been trying to solve: i want to use a txt to randomly pick a car from a txt file but i always get it wrong and i hoped that you could help me here is what i have done so far in this specific part: 我遇到了我一直在试图解决的问题:我想使用txt从txt文件中随机选择一辆汽车,但我总是弄错了,我希望您能在这里为我提供帮助到目前为止,在此特定部分已完成:
import random
if user_input == "1":
with open("cars.txt", "r") as call_uber:
for line in call_uber:
array1 = line.split(" : ")
print(array1)
the objective is to randomly pick the first name of one of the txt file lines but i just managed to split them 目的是随机选择其中一个txt文件行的名字,但我只是设法将它们拆分
ps: i'm a newbie at this. ps:我是这个新手。
To randomly pick a line, just add random.choice function : 要随机选择一条线,只需添加random.choice函数:
import random
if user_input == "1":
with open("cars.txt", "r") as call_uber:
line = random.choice(call_uber.readlines())
array1 = line.split(" : ")
print(array1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.