0

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
19
  • 1
    Which line? Please post the full traceback. Commented Jan 15, 2014 at 23:34
  • my problem is Year has no type Commented Jan 15, 2014 at 23:34
  • 1
    Please post the full traceback. Also, make sure you're returning something from getMonthOfDate. Commented Jan 15, 2014 at 23:36
  • @aIKid i just did the all exception, there is no need for month. pleae check the edited question Commented Jan 15, 2014 at 23:36
  • 1
    The title is silly. Of course you can concatenate two strings. The error message is clearly telling you that one of them isn't a string. Commented Jan 15, 2014 at 23:38

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

9 Comments

Well why didn't you say so? Please post the full traceback for the error
I have edited my answer. In future, if you could post the actual error message, that would be helpful
the month is none. pleae ckech the fuction in the question
You are ridiculous. How are we supposed to help if you can't even decide what the problem is?!
i solve the month problem. now i get this Month = 01 Year = 2014 so the month is not null and the year is not null.
|
2

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)

4 Comments

yes month is none, pleae check the edit question, there is the function
The function returns None when neither of the months is inside the string. Also note that "January " has a trailing space, so if that’s missing, you’re not returning anything either.
the space was the wrong. i found that my self. now i have another problem, please check the question
The same thing applies over and over: You can’t concat strings with non-strings. You have to convert the non-strings first, or use the comma syntax for print as shown in my answer. Also, please do not keep changing your question for new problems.
1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.