str.split Python and Looping question

Questions about programming languages and debugging
Post Reply
User avatar
maboroshi
Dr. Mab
Dr. Mab
Posts: 1624
Joined: 28 Aug 2005, 16:00
18

str.split Python and Looping question

Post by maboroshi »

I am having some issues

I am trying to split a string and use it in iteration with each value
my code is as follows

Code: Select all


.....

target_domain = "suck-oold.com"
target_keywords = "hacking videos, coding, hacking"

.....

target_keyword = target_keywords.split(',')
gs = GoogleSearch(target_keyword)
gs.results_per_page = 100
results = gs.get_results()
for idx, res in enumerate(results):
    parsed = urlparse(res.url)
    domain = mk_nice_domain(parsed.netloc)
    if domain == target_domain:
        print "Ranking position %d for keyword '%s' on domain %s" % (idx+1, target_keyword, target_domain)
I am trying to split the target_keywords

So that GoogleSearch() will loop through them does this make sense

*cheers

Maboroshi

User avatar
leetnigga
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 447
Joined: 28 Jul 2009, 16:00
14

Post by leetnigga »

Umm...

Code: Select all

for target_keyword in target_keywords.split(', '):

User avatar
maboroshi
Dr. Mab
Dr. Mab
Posts: 1624
Joined: 28 Aug 2005, 16:00
18

Damn

Post by maboroshi »

Damn I thought I tried that :-|

Guess I should of waited for the program to finish executing LOL

Thanks anyway leet

*cheers man

User avatar
leetnigga
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 447
Joined: 28 Jul 2009, 16:00
14

Post by leetnigga »

You're welcome.

In cases like this where you're working on a part of a program it's a good idea to find a more general (simpler) problem and solve that, then plug the solution back in.

In this case:

You have a string full of comma-separated keywords that you want to use in a loop.

Code: Select all

>>> target_keywords = "hacking videos, coding, hacking"
First you want to separate those keywords into a list:

Code: Select all

>>> target_keywords.split(', ')
['hacking videos', 'coding', 'hacking']
Then you want to use that list in a loop:

Code: Select all

>>> for target_keyword in target_keywords.split(', '):
...     print(target_keyword)
... 
hacking videos
coding
hacking
Now you have the code you want, namely:

Code: Select all

for target_keyword in target_keywords.split(', '):
Now instead of printing you'll do what you wanted to do originally.

The Python REPL is pretty useful for experiments like these.

User avatar
maboroshi
Dr. Mab
Dr. Mab
Posts: 1624
Joined: 28 Aug 2005, 16:00
18

hey thanks

Post by maboroshi »

Hey thanks good advice man

Coding is all about problem solving. Normally I would have done this, but I guess I was in a hurry or just confused. However good advice

Python REPL? I will have to look that up, for some reason I thought the REPL was the C specification but I am wrong obviously

Thanks man TTYS

User avatar
leetnigga
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 447
Joined: 28 Jul 2009, 16:00
14

Re: hey thanks

Post by leetnigga »

maboroshi wrote:Python REPL? I will have to look that up, for some reason I thought the REPL was the C specification but I am wrong obviously
Oh, sorry. You probably knew what I was talking about, I just used Lisp terminology. I meant the command line program that runs Python code from standard input:

http://docs.python.org/tutorial/interpr ... ctive-mode

REPL is from read-eval-print loop. What it means is basically this:

Code: Select all

while True: # loop
    print(eval(input()))
Which is written like:

Code: Select all

(loop
  (print
    (eval (read))))
in most Lisps, hence the name.

Post Reply