I have this code that manipulates many list (which are a mix of numbers and words) and I need it to print to a text file in rows without brackets. here is the code:

ingroup = open("C:\Python25\Drew\subgroup.txt", 'r')
outgroup = file ("dID.txt", 'w')
ct = 1
rcw = ingroup.readlines()
cID=0
ID=[] #list
dID={} #dictionary
for record in rcw:
bird = str.split(record) # bird is a list now with
dID[bird[3]]=0
for eachid in dID:
dID[eachid]=ct
ct+=1

for record in rcw:
bird = str.split(record)
bird.insert(0,dID[bird[3]])
print >> outgroup, bird

The output looks like this, but I need it without the brackets:

[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']

thank you

Each line of your file is read in as a list, so you need to loop through the list and print each item.

for item in bird:
   outgroup.write(item)

Each line of your file is read in as a list, so you need to loop through the list and print each item.

for item in bird:
   outgroup.write(item)

Hey thanks so much for taking a look at this.
I tried it and I got the following trace back:
Traceback (most recent call last):
File "C:\Python25\Drew\assignID.py", line 19, in <module>
outgroup.write(item)
TypeError: argument 1 must be string or read-only character buffer, not int

Hey thanks so much for taking a look at this.
I tried it and I got the following trace back:
Traceback (most recent call last):
File "C:\Python25\Drew\assignID.py", line 19, in <module>
outgroup.write(item)
TypeError: argument 1 must be string or read-only character buffer, not int

Make sure in the future - you use the "code" block when posting code, it makes it much easier to read...

Whenever you write stuff to file - it needs to be in string format. So when you pass an integer to write, Python errors out. What you want is something like this:

for item in bird:
      outgroup.write(str(item))

You could convert the list to a string and use slice ...

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

# slice off first and last character
str_q = str(q)[1 : -1]
print(str_q)

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

rcw is a list of lists. You want to write strings to a file so you have to add one more level.

for record in rcw:
   bird = record.strip().split()
   ##   add this to each record
   outgroup.write("%s" % (dID[bird[3]])   ## assuming this is a string

   ##   bird is a list
   for field in bird:
      outgroup.write(",  %s" % (field))   ## you may not want the 
comma
   outgroup.write("\n")
##
##   or more simply
for record in rcw:
   bird = record.strip().split()
   ##   prepend this to each record
   outgroup.write("%s  " % (dID[bird[3]]))
   ##  and the record
   outgroup.write("%s" % (record))   ## assumes record already has a "\n"
This question has already been answered. Start a new discussion instead.