我正在使用以下代码来拾取10条随机线,但它也是拾取空行。我只想在做选择时排除空行。并且还希望用*作为前缀标记选定的行。所以下次这段代码不会拾取任何以*开头的行。
import random
task = 10
while ( task >= 0 ):
lines = open('master.txt').read().splitlines()
myline =random.choice(lines)
print(myline)
task -= 1
print ('Done!')
答案 0 :(得分:2)
摆脱空白行:
with open(yourfile) as f:
lines = [ line for line in f if line.strip() ]
您可以修改列表理解的if
部分中的内容以适合您的喜好(例如if line.strip() and not line.startswith('*')
)
现在洗牌并取10:
random.shuffle(lines)
random_lines = lines[:10]
现在,您可以删除通过shuffle
选择的行:
lines = lines[10:]
而不是用*
标记......
答案 1 :(得分:1)
import random
lines = [line
for line in open('master.txt').read().splitlines()
if line is not '']
random.shuffle(lines)
for line in lines[:10]:
print line
print "Done!"
答案 2 :(得分:1)
import random
task = 10
with open('master.txt') as input:
lines = input.readlines()
lines = [line.strip() for line in lines] ## Strip newlines
lines = [line for line in lines if line] ## Remove blank
selected = random.sample(lines, task)
for line in selected:
print(line)
print('Done!')
答案 3 :(得分:1)
以下将随机选择10条非空且未标记的行。打印出选定的行并更新文件,以便标记所选行(前面加*
)并删除空行。
import random
num_lines = 10
# read the contents of your file into a list
with open('master.txt', 'r') as f:
lines = [L for L in f if L.strip()] # store non-empty lines
# get the line numbers of lines that are not marked
candidates = [i for i, L in enumerate(lines) if not L.startswith("*")]
# if there are too few candidates, simply select all
if len(candidates) > num_lines:
selected = random.sample(candidates, num_lines)
else:
selected = candidates # choose all
# print the lines that were selected
print "".join(lines[i] for i in selected)
# Mark selected lines in original content
for i in selected:
lines[i] = "*%s" % lines[i] # prepend "*" to selected lines
# overwrite the file with modified content
with open('master.txt', 'w') as f:
f.write("".join(lines))
答案 4 :(得分:0)
import random
task = 10
#read the lines just once, dont read them again and again
lines = filter(lambda x: x, open('master.txt').read().splitlines())
while ( task >= 0 and len(lines) ):
myline = random.choice(lines)
#don't mark the line with *, just remove it from the list
lines.remove(myline)
print(myline)
task -= 1
print ('Done!')