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

Dani AI

Generated

You do not need to print Python’s list representation at all. Serialize each row to a delimited string (or use the csv module) and write one line per record. This avoids brackets and handles mixed types cleanly.

Example using join (fast and simple for plain text):

# build dID without nested loops, preserving first-seen order
dID = {}
for record in rcw:
    key = record.split()[3]
    if key not in dID:
        dID[key] = len(dID) + 1

for record in rcw:
    bird = record.split()
    row = [dID[bird[3]]] + bird
    outgroup.write(" ".join(map(str, row)) + "\n")  # use "\t" or "," if you prefer

For robust quoting and commas within fields, use the csv module (recommended). On Windows with Python 2.x, open in binary mode to avoid blank lines:

import csv

outgroup = open("dID.txt", "wb")
writer = csv.writer(outgroup, delimiter=",", quoting=csv.QUOTE_MINIMAL)
for record in rcw:
    bird = record.split()
    writer.writerow([dID[bird[3]]] + bird)
outgroup.close()

Notes:

  • join requires strings, so wrap items with map(str, ...).
  • split() without args splits on any whitespace and preserves leading zeros in tokens like "006".
  • Prefer a delimiter like tab ("\t") if fields may contain spaces.

See str.join in the Python docs and the csv module guide for details and edge cases (quoting, newlines).

Recommended Answers

All 6 Replies

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\", 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\", 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"

Thanks everyone
really good advice from all you guys

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.