Marco Dinatsoli
Marco Dinatsoli

Reputation: 10570

cannot concatenate 'str' and 'int' objects

I have this code

Date = site.xpath('my xpath').extract()[0]
            print "DDDDDDDDDDDDDDDDDDDDDD= "+Date
            DateString = Date.split()
            DayString = DateString[0]
            MonthString = DateString[1]
            Year = DateString[2]
            Day = getDayOfDate(DayString)
            Month = getMonthOfDate(MonthString)
            print "Type Year = "+type(Year)
            print "Month  = "+Month+"  Year = "+Year

I got this error

 exceptions.TypeError: cannot concatenate 'str' and 'NoneType' objects

when I print the Year, I got 2014 It seems that the Month is None

this is the exception

**New Exception *****

 exceptions.TypeError: cannot concatenate 'str' and 'int' objects

Upvotes: 0

Views: 2025

Answers (4)

jonrsharpe
jonrsharpe

Reputation: 122032

Back to the original answer:

Month is None because your function returns None because you don't spell "August" correctly. A better function would be:

def getMonthFromDate(s):
    months = ["January", "February", ...] # spell these correctly 
    for index, month in enumerate(months, 1):
        if month in s:
            return "{0:02d}".format(index)
    raise ValueError

Also still a problem:

type(Year)

will return a type object. You cannot add this to a string. That is exactly what the error message (which is not the one you gave) tells you. Try:

print "Type of Year: " + str(type(Year))

Or, as concatenating strings with + is unpythonic, something like:

print "Type of Year: {}".format(type(Year))

These also apply to error three, where you have an int.


You apparently didn't know Python does all of this already: read up on datetime.strptime.

Upvotes: 3

John La Rooy
John La Rooy

Reputation: 304205

One of Month/Year seems to be None. From the code you gave it seems most likely to be Month

If you had used a format string (which is the preferred way) like this

print "Month  = {month}  Year = {year}".format(month=Month, year=Year)

It would not cause an exception, and be immediately clear which one is None

Upvotes: 1

poke
poke

Reputation: 387707

The exception basically says that one operand was a string—as you expected—but the other was None. So you tried doing 'Month = ' + None or similar. So for whatever line this error appeared, the variable you are using there seems to be None instead of an actual string.


In your updated question, the error message is suddenly this:

TypeError: cannot concatenate 'str' and 'type' objects

So you are now trying to concat a type object to a string, which also implicitely doesn’t work. You will have to convert the value to a string first:

print "Type Year = " + str(type(Year))

An alternative way would be to use the functionality of the print statement that allows multiple arguments which are automatically converted to string and concat automatically:

print "Type Year =", type(Year)

Upvotes: 2

Frerich Raabe
Frerich Raabe

Reputation: 94329

It may well be that Year is fine, but Month or Date is not. Consider their values.

Upvotes: 0

Related Questions