Python帮助--->从文本文件中随机选择名称

发布于 2024-12-04 01:20:04 字数 490 浏览 1 评论 0原文

我想在名为“names”的 .txt 文件中列出“john、jack、daniels、whisky、susan、alex”等姓名列表。

现在,我想将该文件导入到我的“脚本”中并使用导入随机模块。

这就是我所拥有的:

import random

name = ( "jay" , "luis" , "bob" , "sofi", "susan" ) 

x = random.sample(name,input( "please enter the number of volunteers needed" ))
print x 

我想要 name = .txt 文件,而不是 name = ( xxxxxxxxxxxxx )

每次我想更改名称时,我都可以在 .txt 文件中更改它。

我正在尝试为我的学校志愿者俱乐部制定一个计划,因此选择的志愿者数量是随机的并且没有偏见。这样每个人都有公平的机会。 :]

I want to have a list of names such as "john, jack, daniels, whisky, susan, alex" in a .txt file called 'names'.

Now, I want to import that file into my 'script' and use the import random module.

This is what I have:

import random

name = ( "jay" , "luis" , "bob" , "sofi", "susan" ) 

x = random.sample(name,input( "please enter the number of volunteers needed" ))
print x 

instead of having name = ( xxxxxxxxxxxxx ), I want to have name = .txt file.

Everytime i want to change the names I can just change it in the .txt file.

I'm trying to make a program for my schools volunteer club, so the number of volunteers chosen is at random and not biased. That way everyone has a somewhat fair chance. :]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

靑春怀旧 2024-12-11 01:20:04
file = open('myFile.txt', 'r')
names = file.read().split(',')
file.close()

使用它代替您的 name = ... 行,您应该可以开始了。

您可以在 此处

请注意,我假设文件中会有一个以逗号分隔的列表。您还可以将每个名称放在单独的行上,然后执行 names = file.readlines() 操作。

file = open('myFile.txt', 'r')
names = file.read().split(',')
file.close()

Use that in place of your name = ... line and you should be good to go.

You can read more about reading and writing files in Python here.

Note that I assumed you'll have a comma-delimited list in the file. You can also put each name on a separate line and do names = file.readlines() instead.

愚人国度 2024-12-11 01:20:04

欢迎使用 python :-]

这样的东西怎么样?

import random
fid = open('names.txt', 'r')
names = fid.readlines()

number_needed = raw_input('please enter the number of volunteers needed: ')

print random.sample(names, int(number_needed))

welcome to python :-]

how about something like this?

import random
fid = open('names.txt', 'r')
names = fid.readlines()

number_needed = raw_input('please enter the number of volunteers needed: ')

print random.sample(names, int(number_needed))
楠木可依 2024-12-11 01:20:04

您可以简单地用由行分隔的名称填充文本文件:

with open('names.txt') as f:
    names = f.read().splitlines()

You could simply fill a text file with names delimited by line:

with open('names.txt') as f:
    names = f.read().splitlines()
疏忽 2024-12-11 01:20:04

假设 names.txt 中有一个名称列表,每行一个(并且其中没有一亿个名称):

import random
number_of_volunteers = 4
print random.sample([n[:-1] for n in open("./names.txt").readlines()], number_of_volunteers)

Assuming a list of names in names.txt, one per line (and that you don't have a hundred million names in there):

import random
number_of_volunteers = 4
print random.sample([n[:-1] for n in open("./names.txt").readlines()], number_of_volunteers)
酒中人 2024-12-11 01:20:04

地点:

$ cat rand_nms.txt
jay
luis
bob
sofi

然后:

import random

contents=[]

with open("rand_nms.txt") as rnd:
    for line in rnd:
        line=line.strip()
        contents.append(line)

print contents
print "random name:", contents[random.randint(0,len(contents)-1)]

结果:

['jay', 'luis', 'bob', 'sofi', 'susan']
random name: luis

Where:

$ cat rand_nms.txt
jay
luis
bob
sofi

Then:

import random

contents=[]

with open("rand_nms.txt") as rnd:
    for line in rnd:
        line=line.strip()
        contents.append(line)

print contents
print "random name:", contents[random.randint(0,len(contents)-1)]

Result:

['jay', 'luis', 'bob', 'sofi', 'susan']
random name: luis
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文