Creating a comma delimited list in python
Question: I have a need for a list of numbers, they need to be hex numbers. The list
needs to be separated by commas (or anything really, just as long as they are
separate) so that I can use the list as input to another program.

I want to start at 0xffffffffff and go to 0xffffffffffffffffffff, printing
each number to a text file, then a comma (separator), and then the next
number, etc. The code I have currently counts, creates a file, and inputs the
numbers.

Tthe problem is it is decimal notation, not hex and it has no seperators. here
is what i have:

    
    
    #!/usr/bin/python
    
    #count in hex from 10 f's to 20 f's and write to comma 
    #delimited file to create a dictionary
    
    def count_hex():
        x = 0xffffffffff
        while x <= 0xffffffffffffffffffff:
           x += 0x1
            s = str(x)
            s.upper()
            with open("dictionary.txt", "a") as diction:
                diction.write(s)
    
    count_hex()
    

I have played around with it by putting in separators, and even found a place
that said i had to use import CSV, but that is something that I do not
understand and would have to change everything.

It seems that since it is mostly working, a few tweaks would be better than
rewriting something that I do not understand.


Answer: First, you reopen the file every time you want to write a number into it. It's
slow. Then, you forget to format the number as hex, and forget to put the
actual comma!

    
    
    def writeHex(start, stop, filename):
      with open(filename, "w") as output:
        for number in xrange(start, stop):
          output.write("%x, " % number)  # writes smth like "ffffff,"
    
    writeHex(0xfffff, 0x100000000000000000000000, "dictionary.txt")
    

Note that all the numbers are written in one line, and there's a comma after
the last number. Adding "\n" if/where needed and adding a variable like
`is_first_number` to avoid the comma placement at the very end are left as an
exercise.

