#Set of lists I want to import into my python program called "setlist.txt"
---------------------------------------------------------------------------------------------
Tripolee = ('Saeed Younan', 'Matrixxman', 'Pete Tong', 'Dubfire', 'John Digweed', 'Carl Cox')
Ranch = ('Dabin', 'Galantis', 'Borgeous', 'Shpongle', 'ODESZA', 'Kaskade')
Sherwood = ('Nadus', 'Mr. Carmack', 'Wave Racer', 'Lido', 'Goldlink', 'Four Tet', 'Flume')
Jubilee = ('Chaz French', 'MartyParty', 'Sango', 'Brodinski', 'Phutureprimitive', 'EOTO')
The Hangar = ('Vourteque', 'The Gentlemen Callers', 'Bart&Baker', 'Jaga Jazzist', 'JPOD')
Forest = ('Vibe Street', 'Lafa Taylor', 'Vaski', 'Little People', 'jackLNDN', 'MartyParty')
---------------------------------------------------------------------------------------------
#program
from sys import exit
from random import randint
from sys import argv
script, setlist = argv
setlist = open(setlist)
print "Here is the setlist for day 1"
print setlist.read()
print "%r is playing on the Tripolee stage" % random.choice(setlist.readline(2))
我在所有这些之间有更多的代码,我没有在这里,但基本上是最后一行,我遇到了麻烦。
答案 0 :(得分:0)
可能不是您文件的最佳格式,但您可以拆分并使用ast.literal_eval:
from ast import literal_eval
with open("in.txt") as f:
choices = [literal_eval(line.split(" = ")[-1]) for line in f]
这将为您提供一个元组列表,您可以将其传递给random.choice:
[('Saeed Younan', 'Matrixxman', 'Pete Tong', 'Dubfire', 'John Digweed', 'Carl Cox'), ('Dabin', 'Galantis', 'Borgeous', 'Shpongle', 'ODESZA', 'Kaskade'), ('Nadus', 'Mr. Carmack', 'Wave Racer', 'Lido', 'Goldlink', 'Four Tet', 'Flume'), ('Chaz French', 'MartyParty', 'Sango', 'Brodinski', 'Phutureprimitive', 'EOTO'), ('Vourteque', 'The Gentlemen Callers', 'Bart&Baker', 'Jaga Jazzist', 'JPOD'), ('Vibe Street', 'Lafa Taylor', 'Vaski', 'Little People', 'jackLNDN', 'MartyParty')]
我不知道setlist应该来自哪里,你的文件看起来像元组赋值。 setlist.readline(2)
会读取2个字节,或者在您的情况下实际上没有任何内容,因为您已经用尽了调用read
的文件迭代器。
我建议在使用literal_eval解压缩后,将文件放在一个更有用的格式中,也可以使用名称作为键创建一个dict并转储dict。
from ast import literal_eval
with open("in.txt") as f:
choices = {}
for line in f:
ven, tpl = line.split(" = ")
choices[ven] = literal_eval(tpl)
print(choices)
输出:
{'Jubilee': ('Chaz French', 'MartyParty', 'Sango', 'Brodinski', 'Phutureprimitive', 'EOTO'), 'Tripolee': ('Saeed Younan', 'Matrixxman', 'Pete Tong', 'Dubfire', 'John Digweed', 'Carl Cox'), 'The Hangar': ('Vourteque', 'The Gentlemen Callers', 'Bart&Baker', 'Jaga Jazzist', 'JPOD'), 'Ranch': ('Dabin', 'Galantis', 'Borgeous', 'Shpongle', 'ODESZA', 'Kaskade'), 'Sherwood': ('Nadus', 'Mr. Carmack', 'Wave Racer', 'Lido', 'Goldlink', 'Four Tet', 'Flume'), 'Forest': ('Vibe Street', 'Lafa Taylor', 'Vaski', 'Little People', 'jackLNDN', 'MartyParty')}
您可以使用json.dump
或pickle
模块保留字典,这样您的数据每次都会以更简单的格式显示。
为了让您更清楚以下内容,您的.txt文件的内容是:
---------------------------------------------------------------------------------------------
Tripolee = ('Saeed Younan', 'Matrixxman', 'Pete Tong', 'Dubfire', 'John Digweed', 'Carl Cox')
Ranch = ('Dabin', 'Galantis', 'Borgeous', 'Shpongle', 'ODESZA', 'Kaskade')
Sherwood = ('Nadus', 'Mr. Carmack', 'Wave Racer', 'Lido', 'Goldlink', 'Four Tet', 'Flume')
Jubilee = ('Chaz French', 'MartyParty', 'Sango', 'Brodinski', 'Phutureprimitive', 'EOTO')
The Hangar = ('Vourteque', 'The Gentlemen Callers', 'Bart&Baker', 'Jaga Jazzist', 'JPOD')
Forest = ('Vibe Street', 'Lafa Taylor', 'Vaski', 'Little People', 'jackLNDN', 'MartyParty')
要打印场地和设置列表,您可以使用dict.items:
for ven, set_l in choices.items():
print("Set list for {}: {}".format(ven, ", ".join(set_l)))
输出:
Set list for Jubilee: Chaz French, MartyParty, Sango, Brodinski, Phutureprimitive, EOTO
Set list for Tripolee: Saeed Younan, Matrixxman, Pete Tong, Dubfire, John Digweed, Carl Cox
Set list for The Hangar: Vourteque, The Gentlemen Callers, Bart&Baker, Jaga Jazzist, JPOD
Set list for Ranch: Dabin, Galantis, Borgeous, Shpongle, ODESZA, Kaskade
Set list for Sherwood: Nadus, Mr. Carmack, Wave Racer, Lido, Goldlink, Four Tet, Flume
Set list for Forest: Vibe Street, Lafa Taylor, Vaski, Little People, jackLNDN, MartyParty
当您打开文件并致电read
时,您现在将文件中的所有内容存储为字符串。然后打印字符串,接下来尝试random.choice(setlist.readline(2))
,readline(2)
试图读取两个字节,它甚至不能做,因为文件指针位于文件的末尾,因为你已经调用了{{1}所以你看到一个空字符串输出。
如果你想从第一个元组中获取一个随机字符串:
read