On 9/12/05, Tom Tucker <[EMAIL PROTECTED]> wrote:
Tutor,

Good evening!    The goal is to parse a simple file and grab column one. 
Then print each value horizontally separated by a comma.
Why is Python adding a space padding between each value?   Please see below.
Thanks ahead of time.




INPUT_FILE # unwanted lines removed
################################
5555    [EMAIL PROTECTED]    blah    blah
1111    [EMAIL PROTECTED]    blah    blah
3333    [EMAIL PROTECTED]    blah    blah
4444    [EMAIL PROTECTED]    blah    blah


OUTPUT DESIRED
##################
5555,1111,3333,4444


SCRIPT
#########
import re

input_file = open('/tmp/file','r')
number_match = re.compile('^\d+\s+\w+\@')
for line in input_file.readlines():
        if number_match.match(line):
                line = re.split('\s+', line)
                print line[0],
                print ",",


OUTPUT GENERATED
####################
5555 , 1111 ,  3333 , 4444 ,       


To add to Danny's posting, I recently had to stream out an unknown list of values from a database, something like what you are doing.  I created a class that formats the lines like you want and writes them out when the line gets close the the maximum length.

class Line:
    length = 72
    seperator = ', '
    def __init__(self):
        self.reset()
    def reset(self):
        self.contents = ''
    def __str__(self):
        return str(self.contents)
    def __len__(self):
        return len(self.contents)
    def __add__(self, other):
        o = str(other)
        l = len(o)
        s = len(self.seperator)
        if len(self) + l + s > self.length:
            self.flush()
        if self.contents:
            self.contents += self.seperator
        self.contents += str(other)
        return self
    def flush(self):
        if self.contents:
            self._print(self.contents)
        self.reset()
    def _print(self, line):
        print line

For your purposes, you would want to change the separator to just ','.  Then you would just create an instance and add strings to it.
formatted_line = Line()
for line in input_file.readlines():
    if number_match.match(line):
        line = re.split(r'\s+', line)
        formatted_line += line[0]
else:
    formatted_line.flush()  # clear anything left to print

HTH,
  -Arcege
--
There's so many different worlds,
So many different suns.
And we have just one world,
But we live in different ones.
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to