Reading and writing an unknown character into an appropriately named file I would like to refactor this code a bit further and make it better and more generic.

As of right now, it is doing what I want it to do (reading a list of URL's, splitting the query and the ampersand and writing the split into appropriately named files). However, I would like to take the hash list out and make it as generic as possible. I guess what I mean is, if the loop finds a character, read what the character is (i.e: a=12(character would be 'a')), have Python create a text file for it ('a_file.txt') and append into the newly created text file. So, there was not file there before, but there is now. And Python would do this for every character past the query (http://www.asdasd.com/asds/asd?a=6&amp;b=12&amp;c=123 would create 'a_file.txt', 'b_file.txt', 'c_file.txt').

I feel as if I am asking the right question, but correct me if I am wrong.

import urlparse
def parse_file(input_file):
  tags = {tag: tag + '_file.txt' for tag in {'blog',
        'p','attachment_id','lang',
        'portfolio','page_id','comments_popup',
        'iframe','width','height'}}
with open(input_file) as input:
for line in input:
    parsed_url = urlparse.parse_qsl(urlparse.urlparse(line).query)
    if parsed_url &gt; 0:
        for key, value in parsed_url:
            if key in tags:
                with open(tags[key], 'a') as output_file:
                    output_file.write(value)
            else:
                print key + " not in tags."
    else:
        print(line + " does not yield query.")
parse_file()


Once again, correct me if I am wrong or if my question is confusing.
This is only partly an answer, intended more as a thought exercise about what you might do. I provide a couple examples that you might want to pick and choose parts of, but you probably will not want to just grab the whole. So let's dive in.

What would benefit from this being more general? This seems like a fairly small and specific task. I could imagine parameterizing the input, the list of tags, the mapping from tag name to file name, or the handling once a matched tag is found. Let's examine doing all at once:

import urlparse
def parse_queries(input, get_filename, handle, tags={'blog', 'p', ...}):
    for line in input:
        qsl = urlparse.parse_qsl(urlparse.urlparse(line).query)
        if not qsl:
            print repr(line), "does not yield query"
            continue
        for key, value in qsl:
            if key in tags:
                handle(get_filename(tag), value)
            else:
                print key, "not in tags."

def dot_text(tag):
    return tag + '_file.txt'

def append_to_file(filename, value):
    with open(filename, 'a') as output_file:
        output_file.write(value)

with open("filename") as input:
    parse_queries(input, get_filename=dot_text, handle=append_to_file)


Do any of these help more than they harm readability? Does extracting the small methods like dot_text and append_to_file offer useful chunks of reusable code? I'm not sure myself. It certainly makes the call harder, but providing defaults is easy enough. Thinking it through should raise other questions, such as whether get_filename should be part of parse_queries, or part of handler.

Perhaps it's the line processing which could be parameterized:

def parse_file(input_file, parse):
    ...
    for line in input:
        for key, value in parse(line):
            if key in tags:
                ...

def extract_query_params(line):
    qsl = urlparse.parse_qsl(urlparse.urlparse(line).query)
    if not qsl:
        print repr(line), "does not yield query"
    return parsed_url

parse_file("filename", extract_query_params)


But again ask whether this helps generality more than it harms readability. I'd address other small issues first:


comparing a list to 0 (parsed_url &gt; 0) doesn't have much meaning.
output_file.write(value) seems unlikely to insert newlines, so your values will blur together.
the name tags has a lot of implied meaning in an HTML context, so your use here in an HTTP context could be confusing.
indentation, at least here, your with statement is at the same level as its contained for loop.
unit tests; how better to verify whether this does what you want? Of course if you do want to test this, you may find that parameterizing things makes this measurably easier. Or maybe you really need to look at the resulting files.