1

I'm working on a script, see here:

from pysnap import Snapchat
import time

usnlist = raw_input('Enter file name: ')

s = Snapchat()

def filter(username, password):
    s.login(username, password)
    with open (usnlist, 'r') as file:
        for line in file:
            ok = False
            while not ok:
                try:
                    resp = s.add_friend(line.rstrip("\n"))
                    if 'object' in resp.keys():
                        # 'object' is contained within resp as a key
                        if resp['object']['type']:
                            # type is 1
                            with open ('private.txt', 'a') as f: f.write(resp['object']['name']+"\n")
                            print line.rstrip("\n") + "'s",
                            print "privacy settings are set to: FRIENDS"
                        else:
                            # type is 0
                            with open ('n-private.txt', 'a') as f: f.write(resp['object']['name']+"\n")
                            print line.rstrip("\n") + "'s",
                            print "privacy settings are set to: EVERYONE"
                        s.delete_friend(line)
                    else:
                        # no object in resp, so it's an invalid username
                        print line.rstrip("\n") + " is not a valid username"
                    ok = True
                except:
                    time.sleep(5)
                    print "SNAPCHAT SERVER OVERLOAD - HOLD ON."

username = raw_input('Enter username: ')
password = raw_input('Enter password: ')

filter(username, password)

What I want now is be able to input a value lets call it n And when I input n the bot scrapes only every n line.

For example. N = 2. The bot now only scrapes and inputs every second username from the list.

I came up with some stuff, like adding [0::2] to: resp = s.add_friend(line.rstrip("\n")) Resulting in: resp = s.add_friend(line[0::2].rstrip("\n"))

But that did not work, bot went straight to print "SNAPCHAT SERVER OVERLOAD - HOLD ON." Without checking any names.

I got the idea here:

http://stackoverflow.com/questions/18155682/gathering-every-other-string-from-list-line-from-file

But the info given was not enough for me to get this to work. And I use python 2.7.8

I wish there was just a way to tell python: "Get every n line in file" Because that is basically what I am looking for.

If someone could help that'd be great!

Snowlav
  • 325
  • 1
  • 12
  • 26

2 Answers2

3

What you tried slices the line, not the file. Files don't have slice support, but the itertools module has a function for slicing arbitrary iterables:

import itertools
for line in itertools.islice(file, None, None, n):
    do_whatever_with(line)

The first two arguments are start and stop; a value of None for these arguments means the start and end of the input, since you can't omit them like you could in a regular slice. The third argument is the step.

user2357112
  • 260,549
  • 28
  • 431
  • 505
1

To get every nth line in the file, replace this:

for line in file:

with:

for line in file.read().splitlines()[0::n]:
Gerrat
  • 28,863
  • 9
  • 73
  • 101