tags:

views:

126

answers:

3

I have run into a problem joining two strings in Python. I have some code that is like this:

for line in sites:
    site = line        

    for line in files:
        url = site+line

That should be easy I thougth but the strings ends up "looking wierd":

http://example.com/ (this is the site) history.txt (Then the line comes on another "line" in the strings which screws it up when I try to open the url because it is invalid)

Anyone knows a solution?

+2  A: 

The simplest thing is to avoid using the same variable in the for statements:

for site in sites:
  for line in files:
     url = site + line

Does that clear things up? It is good practice in any case.

Kathy Van Stone
if "sites" is a file the line ending \n\r is NOT stripped by the for construct
fuzzy lollipop
Thanks, a combination of that and gnibbler's answer solved the problem.
pyCtrl_
A: 

Perhaps the problem is in using the identifier name 'line' twice?

rmn
+1  A: 

Maybe you have extra whitespace for example a newline at the end of the site

for site in sites:
    for line in files:
        url = site.strip() + line.strip()
gnibbler