Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'_csv.writer' object has no attribute 'write'

Tags:

python

csv

I am not sure what the problem is here. I have a csv file I want to filter. I want to remove all lines starting with '@' and all lines where the third column is the string 'chrM'. Im basically setting my code up to be like the answer here:

TypeError: expected a character buffer object

But Im getting an error.

import re
import csv

inputSamFile = 'excerpt'
outSamFile = 'filternoM'

with open(inputSamFile) as inputSam, open(outSamFile, 'wt') as outSam:
    inputSamCont = csv.reader(inputSam, delimiter = '\t')
    outSamCont = csv.writer(outSam, delimiter = '\t')
    for line in inputSamCont:
        if line[0].startswith('@'):
            continue
        elif line[2] == 'chrM':
            continue
        else:
            outSamCont.write(line)

Traceback (most recent call last): File "filterMito.py", line 19, in outSamCont.write(ProcessLine(line)) AttributeError: '_csv.writer' object has no attribute 'write'

What am I doing wrong

like image 534
von Mises Avatar asked Dec 17 '13 16:12

von Mises


2 Answers

You may be looking for .writerow().

I also ran into this problem, as the documentation I was following used .write(), but csv.writer objects use .writerow().

like image 53
code4meow Avatar answered Oct 20 '22 16:10

code4meow


The error tells you everything you need to know.

AttributeError: '_csv.writer' object has no attribute 'write'

In your code, you create the object:

outSamCont = csv.writer(outSam, delimiter = '\t')

then try to call the .write() method:

outSamCont.write(line)

(or, as it is in the traceback

outSamCont.write(ProcessLine(line)) 

I'm not sure why you have posted different code to what you're running).

However, that object, a csv.writer, does not have the method write, hence the error message. See the documentation for csv.writer objects for the list of methods they do have, and choose the appropriate one.

like image 34
jonrsharpe Avatar answered Oct 20 '22 16:10

jonrsharpe