Question

I have code like this. I am trying to remove the first line of the tsv file which have a field names, say field1, field2,.., fieldn.

  1. is something wrong with this piece of code. I dont get the dialect part right. At present it gives an AttributeError: 'function' object has no attribute 'readline.

  2. Also is there a way I can give some field names from the header for it to read. I tried doing that with a csv.Sniffer().has_header method with no luck.

Please help

with open('outfile.txt','rb') as tsvin:
  dialect=csv.Sniffer().sniff.readline(1024)
  tsvin.seek(0)
  reader=csv.reader(tsvin,dialect,delimiter='\t')
  #has_header=csv.Sniffer().has_header(inf.read(1024))

for row in tsvin:
  tsid= row[0]
  full_list.append(tsid)
  print [(g[0],len(list(g[1]))) for g in itertools.groupby(full_list)]
Was it helpful?

Solution

I got this right:

data= csv.reader(open('outfile.tsv','rb'), delimiter = "\t")
fields=data.next()
for row in data:
         tsid=row[0]
         full_list.append(tsid)
print [(g[0],len(list(g[1]))) for g in itertools.groupby(full_list)]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top