File iteration Issue

This is the place for queries that don't fit in any of the other categories.

File iteration Issue

Postby slipcell » Fri Oct 25, 2013 12:50 pm

Can anyone help me with the following?

Why doesn't the output file contain all URLs rather than just the last?

For some reason it's not iterating over each URL in the file and printing to the new file

Code: Select all
from urlparse import urlparse

with open('urls.txt') as f:
    urls = f.readlines()

for url in urls:
    parse_object = urlparse(url)
    domain = parse_object.netloc
       
       
f = open("disavow.txt", "w").writelines(domain)
slipcell
 
Posts: 8
Joined: Fri Oct 18, 2013 1:03 pm

Re: File iteration Issue

Postby Mekire » Fri Oct 25, 2013 1:28 pm

You are overwriting the value in domain every time through your for loop. Instead you need to keep them all in a list or something.

This perhaps:
Code: Select all
domains = [urlparse(url).netloc for url in urls]

with open("disavow.txt", "w") as outfile:
    outfile.writelines(domains)

-Mek
User avatar
Mekire
 
Posts: 1025
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: File iteration Issue

Postby slipcell » Fri Oct 25, 2013 1:40 pm

Thanks, that is working but printing to one line, should '.writelines' not write to lines??
slipcell
 
Posts: 8
Joined: Fri Oct 18, 2013 1:03 pm

Re: File iteration Issue

Postby slipcell » Fri Oct 25, 2013 1:43 pm

Never mind sorted it!!

Code: Select all
with open('urls.txt') as f:
    urls = f.readlines()

for url in urls:
    parse_object = urlparse(url)
    domains = [urlparse(url).netloc for url in urls]

with open("disavow.txt", "w") as outfile:
    outfile.writelines('\n'.join(domains))


Thanks mate
slipcell
 
Posts: 8
Joined: Fri Oct 18, 2013 1:03 pm

Re: File iteration Issue

Postby Mekire » Fri Oct 25, 2013 2:07 pm

Ah.. the urlparse strips off the original newline character of course; I didn't consider. Anyway, you're now iterating over your list one more time than you need to. And also the writelines method just does multiple writes for every item in a sequence. If you are using join you should just use write.

-Mek
User avatar
Mekire
 
Posts: 1025
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: File iteration Issue

Postby slipcell » Fri Oct 25, 2013 2:15 pm

You sir are a genius :)
slipcell
 
Posts: 8
Joined: Fri Oct 18, 2013 1:03 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 7 guests