Why does a 'WriteOnlyWorksheet' object have no attribute 'cell'? - python

import openpyxl
wb=openpyxl.Workbook("multiplication.xlsx")
wb.create_sheet()
sheet=wb.get_active_sheet()
sheet.cell(column=6, row=4).value= 5
wb.save("multiplication.xlsx")
When i try and write in the cell, I receive this error.
Traceback (most recent call last):
File "/Users/bjg/Desktop/excel2.py", line 8, in <module>
sheet.cell(column=6, row=4).value= 5
AttributeError: 'WriteOnlyWorksheet' object has no attribute 'cell'
I was wondering if anybody knew why this was the case?

From the write-only mode docs:
In a write-only workbook, rows can only be added with append(). It is not possible to write (or read) cells at arbitrary locations with cell() or iter_rows().

Instead of doing:
wb=openpyxl.Workbook("multiplication.xlsx")
just do:
wb=openpyxl.Workbook()
then at last save with:
wb.save("multiplication.xlsx")

Related

Python-Weka-Wrapper3 removing attributes from arff file error

I have an arff file and I need to remove the first 5 attributes from it (without manually deleting them). I tried to use the Python-Weka-Wrapper3 as it is explained here which enables the filtering options of Weka, however I get an error while using the following code:
import weka.filters as Filter
remove = Filter(classname="weka.filters.unsupervised.attribute.Remove", options=["-R", "1,2,3,4,5"])
The error that I receive is the following:
Traceback (most recent call last):
File "/home/user/Desktop/file_loading.py", line 16, in <module>
removing = Filter(classname="weka.filters.unsupervised.attribute.Remove", options=["-R", "last"])
TypeError: 'module' object is not callable
What could be the reason for this error? Also I would appreciate if anyone knows an alternative way to remove attributes from an arff file using Python.
You are attempting to call the module object instead of the class object.
Try using:
from weka.filters import Filter
remove = Filter(classname="weka.filters.unsupervised.attribute.Remove", options=["-R", "1,2,3,4,5"])

TypeError: 'int object has no attribute '__getitem__' ... but where?

For a Markov chain project I am writing, I am generating the error at the top of the page. I know that this means I am trying to call a list method on an integer object, which must means I am either a) not initializing a list correctly or b) overwriting a list with an integer value at some point in the program. However, I have been trying to debug this for hours now and cannot find the issue in my small program. The error trace is as follows:
Traceback (most recent call last):
File "/Users/adamlind/PycharmProjects/Capstone/Song.py", line 9, in <module>
musicMarkov.add(["c", 4]) #row
File "/Users/adamlind/PycharmProjects/Capstone/Music.py", line 19, in add
self._markov.add(iNote[0], fNote[0])
File "/Users/adamlind/PycharmProjects/Capstone/Markov.py", line 22, in add
self._adjMatrix[val[iVal]][val[fVal]] += 1
TypeError: 'int' object has no attribute '__getitem__'
And here is a link to the GitHub repo containing my project (this is my first StackOverflow question, not sure if this is frowned upon or not):
https://github.com/adamlind323/CSC493
I checked through a few times but couldn't figure out where I am overwriting. I'm not very experienced with Python, so any and all help with this would be greatly appreciated.
Thanks!
val is int. Attempt to index (val[iVal]) leads to exception.
On line #21 you intialize val in the following way:
val = self._lookupVal[iVal]
_lookupVal is a dictionary defined on line #11 which contains integers (see line #14)

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

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
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().
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.

Python 3.2 skip a line in csv.DictReader

How do I skip a line of records in a CSV when using a DictReader?
Code:
import csv
reader = csv.DictReader(open('test2.csv'))
# Skip first line
reader.next()
for row in reader:
print(row)
Error:
Traceback (most recent call last):
File "learn.py", line 3, in <module>
reader.next()
AttributeError: 'DictReader' object has no attribute 'next'
You use next(reader) instead.
Source: csv.DictReader documentation
Since Python 2.6 you should use next(foo) instead of foo.next().
It was considered a mistake in python2 to have the method called next() instead of __next__()
next(obj) now calls obj.__next__() just like str, len etc. as it should.
You usually wouldn't call obj.__next__() directly just as you wouldn't call obj.__str__() directly if you wanted the string representation of an object.
Handy to know if you find yourself writing unusual iterators

Problems using PIL 1.1.7

The trivial
import Image
im = Image.OPEN('C:\abc.bmp')
results in the following exception
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
im = Image.OPEN('C:\Documents and Settings\umair.ahmed\My Documents\My Pictures\avanza.bmp')
TypeError: 'dict' object is not callable
not sure if i am missing something, kindly help.
Use:
Image.open()
It's case-sensitive.
I don't think the error message came from your input, because the file names are different, but you should not use 'C:\abc.bmp', in your open() call, but use either C:/abc.bmp, or r'C:\abc.bmp'. Backslash is an escape character in Python.

Categories