TypeError

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

TypeError

Clark, Kathleen

Hello,

 

I am new to python and am trying to correct the follow error:

 

TypeError: sequence item 1: expected string, NoneType found

 

The error message is referencing line 86 of my code:

 

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

 

 

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 anyone could help me understand what the error is and needs to be done to correct it, I think I might be able to fill in the blanks.

 

Thanks,

 

Katie

 

 

 

______________________________________________________________________

logo for email

                  Katie Clark

            Research Assistant

                  SHARRPP

    60 Temple Street, Suite 4D

         New Haven, CT 06510

               203-737-7425

           [hidden email]

             www.sharrpp.com

 

 

 


--
http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|

Re: TypeError

Dave Angel-3
On 02/01/2012 11:53 AM, Clark, Kathleen wrote:
> Hello,
Which python version, what operating system.  Doesn't cost much to
specify, and can frequently be relevant.
>
> I am new to python and am trying to correct the follow error:
>
> TypeError: sequence item 1: expected string, NoneType found
>
That's not an error message, it's just the last line of one.  Please use
copy/paste to post the entire traceback into your query.
> The error message is referencing line 86 of my code:
>
> ws.cell(row=row, column=1).value = ','.join([str(ino), fn, ln, sdob])
>
And this couldn't be simplified?  The sample is not runnable, so we have
to make up a wrapper program to define at least 6 variables, and then
execute this line?
> If I'm understanding this correctly, the code
Which 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 anyone could help me understand what the error is and needs to be done to correct it, I think I might be able to fill in the blanks.
>
> Thanks,
>
> Katie
>
>
If I guess you're running Python 2.7 on Linux 11.04, I could try the
following:

 >>> ino = 4
 >>> fn = None
 >>> ln = 12
 >>> sdobj = object()
 >>> '.'.join([str(ino), fn, ln, sdobj)
   File "<stdin>", line 1
     '.'.join([str(ino), fn, ln, sdobj)
                                      ^
SyntaxError: invalid syntax
 >>> '.'.join([str(ino), fn, ln, sdobj])
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: sequence item 1: expected string, NoneType found
 >>>

If this matches your circumstance, the problem is that fn has a value of
None.  You could have guessed this by simply tucking some print
statements right in front of the offending line, displaying all six
variables, and seeing which is of type NoneType.

So now you have to figure out how fn got that value.

(please don't post graphic attachments to your message, this is a text
mailing list)



--

DaveA

--
http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|

Re: TypeError

Nick Dokos
In reply to this post by Clark, Kathleen
Clark, Kathleen <[hidden email]> wrote:



> TypeError: sequence item 1: expected string, NoneType found
>
> The error message is referencing line 86 of my code:
>
> ws.cell(row=row, column=1).value = ','.join([str(ino), fn, ln, sdob])
>
> 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.

I'd guess that the sequence in question is the list that's the argument
of join(), in which case item 1 is fn: it should be a string but for some
reason, it is a None value:

,----
| >>> ','.join([str(45), None, "bar", "foo"])
| Traceback (most recent call last):
|   File "<stdin>", line 1, in <module>
| TypeError: sequence item 1: expected string, NoneType found
|>>> ','.join([str(45), "a string", "bar", "foo"])
|'45,a string,bar,foo'
`----

Nick

--
http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|

TypeError

Clark, Kathleen
In reply to this post by Clark, Kathleen

Hello and thank you for all responses.  I have resolved my problem.  Turned out that one of the files was missing “fn” and after deleting the record, the program ran just fine.

 

Thanks again,

 

Katie

 

 


--
http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|

Re: TypeError

Steven D'Aprano-11
In reply to this post by Clark, Kathleen
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>&nbsp;</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>&nbsp;</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
--
http://mail.python.org/mailman/listinfo/python-list