TypeError

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Feb 1 19:32:42 EST 2012


Hello Katie,

When posting to technical news groups like this, can you please send 
plain text emails rather than so-called "rich text" (actually HTML code) 
email? Or at least, don't use Microsoft Word as your email editor. Many 
people will be reading your posts using text applications and will see 
line after line of very bad HTML like this:

> <p class="MsoNormal">Hello,<o:p></o:p></p> <p
> class="MsoNormal"><o:p> </o:p></p> <p class="MsoNormal">I am new to
> python and am trying to correct the follow error:<o:p></o:p></p> <p
> class="MsoNormal"><o:p> </o:p></p> <p
> class="MsoNormal"><b>TypeError: sequence item 1: expected string,
> NoneType found<o:p></o:p></b></p> [...]

and that's the sort of thing that makes programmers grumpy and less 
inclined to help.

To answer your question:


On Wed, 01 Feb 2012 16:53:41 +0000, Clark, Kathleen wrote:
> I am new to python and am trying to correct the follow error:
> 
> TypeError: sequence item 1: expected string, NoneType found

It is best, if possible, to post the entire traceback starting with the 
line "Traceback (most recent call last)" rather than just the last line. 
In this case, the error is simple enough to work out that it may not 
matter, but in general it often does matter.


> The error message is referencing line 86 of my code:
> 
> ws.cell(row=row, column=1).value = ','.join([str(ino), fn, ln, sdob])

In this case, one of the variables fn, ln and sdob is None instead of a 
string.


> If I'm understanding this correctly, the code is expecting a string, but
> not finding it.  I'm wondering, what is meant by a "string" and also how
> I can figure out the problem and correct it.

If you are new enough to programming that you need to ask what is a 
string, you might find the Python tutor mailing list a more useful place 
than here. 

http://mail.python.org/mailman/listinfo/tutor

"String" is short for "string of characters", that is, text, and in 
Python strings are created with quotation marks or the str() function. So:

a = "this is a string"
b = 'so is this'
c = str(some_variable)  # and so is this

To fix the problem, there is a quick and dirty way, and the proper way. 
The quick and dirty way is to just force all items in the list to be 
strings, regardless of what they currently are. Your line:

ws.cell(row=row, column=1).value = ','.join([str(ino), fn, ln, sdob])

becomes:

ws.cell(row=row, column=1).value = ','.join(
    [str(ino), str(fn), str(ln), str(sdob)])


While this might work, the fact that one or more of fn, ln and sdob is 
None may be a bug in your code, and converting to a string may very well 
just obscure the source of the bug and make it harder to solve in the 
long run. So the proper way to fix the problem is:

(1) Identify which variable is not a string.
(2) Find out why it becomes set to None.
(3) Fix it.

The first part is easy: insert this line before line 86:

print("fn, ln, sdob:", fn, ln, sdob)

and see what it says. Fixing it means working backwards from that point, 
finding out where the variable gets set to None, and fixing that bug.

Good luck!


-- 
Steven



More information about the Python-list mailing list