user1582596
user1582596

Reputation: 503

Python empty line and text marking

I am using following code to pickup 10 random lines, however it's also pickups empty line. I just wanted to exclude empty line while doing selection. and also wants to mark selected line with * as prefix. so next time this code will not pickup any line which start with *.

import random  
task = 10  
while ( task >= 0 ):  
    lines = open('master.txt').read().splitlines()
    myline =random.choice(lines)    
    print(myline)  
    task -= 1  
print ('Done!')

Upvotes: 0

Views: 593

Answers (5)

Andy Hayden
Andy Hayden

Reputation: 375445

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!"

Upvotes: 1

Shawn Chin
Shawn Chin

Reputation: 86854

The following will randomly select 10 lines that are non-empty and not marked. The selected lines are printed out and the file is updated such that the selected lines are marked (prepended with *) and empty lines removed.

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))

Upvotes: 1

mgilson
mgilson

Reputation: 309891

To get rid of blank lines:

with open(yourfile) as f:
    lines = [ line for line in f if line.strip() ]

You can modify the stuff in the if part of the list comprehension to suit your fancy (if line.strip() and not line.startswith('*') for example)

Now shuffle and take 10:

random.shuffle(lines)
random_lines = lines[:10]

Now you can remove the lines you selected via shuffle with:

lines = lines[10:]

Rather than marking with a * ...

Upvotes: 2

Rob Cowie
Rob Cowie

Reputation: 22619

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!')

Upvotes: 1

thikonom
thikonom

Reputation: 4267

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!')

Upvotes: 0

Related Questions