I need to add a column to a list of strings and then print it back to a new text file without [] or "". I tried this below but it is not the result I wanted.

infile looks like this:
23 CL 006 2004 DBA8ORPU 41 8Y S0111P 2
53 CL 006 2004 LBA8PUYE 32 10Y S0111P 2
39 CL 006 2004 A8LGPUYE 10 TY S0111P 2

rcw = infile.readlines()
for record in rcw:
    bird = str.split(record)
    bird.insert(7,'0')
    age=bird[6]
    if age=='HY':
        bird[7]='0.1'
    elif age=='SY':
        bird[7]='1'
    elif age=='TY':
        bird[7]='2'
    else:
        pass
    print >> outfile, bird

The outfile looks like this, but I need it to look like the infile with the new column and no '' or []:

As long as there is no comma in your individual data elements, you can use function join() and replace() in this manner:

q = ['23', 'CL', '006', '2004', 'DBA8ORPU', '41', '8Y', '0', 'S0111P', '2']

s = ",".join(q).replace(",", " ")

print(s)

"""
my result -->
23 CL 006 2004 DBA8ORPU 41 8Y 0 S0111P 2
"""

Thanks ene uran
worked great

As long as there is no comma in your individual data elements, you can use function join() and replace() in this manner:

q = ['23', 'CL', '006', '2004', 'DBA8ORPU', '41', '8Y', '0', 'S0111P', '2']

s = ",".join(q).replace(",", " ")

print(s)

"""
my result -->
23 CL 006 2004 DBA8ORPU 41 8Y 0 S0111P 2
"""

And why not directly :

s = " ".join(q)

print(s)

"""
my result -->
23 CL 006 2004 DBA8ORPU 41 8Y 0 S0111P 2
"""

Thanks jice! It was late in the day and I was thinking CSV rather than lists.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.