2016-03-19 14 views
3

このエラーが発生しています。フォーマッタで問題が発生しました:「%: 'NoneType'と 'tuple'のサポートされていないオペランドタイプ」

私はGmailのAPIを使用することを学んだからその例を貼り付けコピーします。ここではhttps://developers.google.com/gmail/api/v1/reference/users/threads/get#examples

はコードです:

def GetThread(service, user_id, thread_id): 
    """Get a Thread. 

    Args: 
    service: Authorized Gmail API service instance. 
    user_id: User's email address. The special value "me" 
    can be used to indicate the authenticated user. 
    thread_id: The ID of the Thread required. 

    Returns: 
    Thread with matching ID. 
    """ 
    try: 
    thread = service.users().threads().get(userId=user_id, id=thread_id).execute() 
    messages = thread['messages'] 
    print ('thread id: %s - number of messages ' 
      'in this thread: %d') % (thread['id'], len(messages)) 
    return thread 
    except errors.HttpError, error: 
    print 'An error occurred: %s' % error 

私は現在、このエラーを取得しています:

thread id: %s - number of messages in this thread: %d 
Traceback (most recent call last): 
    File "quickstart1.py", line 176, in <module> 
    main() 
    File "quickstart1.py", line 152, in main 
    GetThread(service, EMAIL_LOGIN, thread_id) 
    File "quickstart1.py", line 121, in GetThread 
    'in this thread: %d') % (thread['id'], len(messages)) 
TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple' 

変更しても print ('thread id: %s - number of messages in this thread: %d') % (thread['id'], len(messages))

へ:print ('thread id: %s - number of messages in this thread: %d') % ('test', 1)

thread id: %s - number of messages in this thread: %d 
Traceback (most recent call last): 
    File "quickstart1.py", line 175, in <module> 
    main() 
    File "quickstart1.py", line 151, in main 
    GetThread(service, EMAIL_LOGIN, thread_id) 
    File "quickstart1.py", line 120, in GetThread 
    print ('thread id: %s - number of messages in this thread: %d') % ('test', 1) 
TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple' 

私はまだ同じエラーを取得します。どんなアイデア理由?

+0

''%'の後に ')'を行末まで移動する必要があります。 – zondo

+0

['format'](https://docs.python.org/2/library/stdtypes.html#str.format)の代わりに'% 'を使用する特別な理由はありますか? – MSeifert

+1

この投稿を開いておくと、将来の訪問者がGoogleの明示的なPythonのバージョンステートメントがないため混乱するのを助けるでしょう。最近、Python 3を使用する人が増え、同じ問題が発生する可能性があります。これは、OPが作ったタイプミスではありません。 –

答えて

2

あなたのかっこの位置が間違っています。

print ('thread id: %s - number of messages in this thread: %d') % ('test', 1) 

あなたはNoneを返す、printを呼んでいます。次に、 に%を適用しようとしています。それはNoneとタプルの間です。それは次のようになります。

print ('thread id: %s - number of messages in this thread: %d' % ('test', 1)) 

編集:マルタインは正しいです。これはPython 2です。私は というPython 3を考えていました。これは私が使用する唯一のものです。

これがPython 3の場合は、printが関数になり、かっこ はすべてを回り込む必要があります。 Python 3でこれを実行すると、 そのエラーが発生します。

しかし、Python 2ではprintがステートメントで、 カッコを使用する必要はありません。

print ('thread id: %s - number of messages ' 
     'in this thread: %d') % (thread['id'], len(messages)) 

ここで、かっこは異なる目的を果たします。文字列は2行にまたがって に分割され、パーサーはそれを1つのオブジェクトとして として扱うために括弧が必要です。 'strings' 'like' 'this'は、1つの 文字列に連結されます。したがって、Python 2では、これは正しい構文です。

+0

gaaah! ..... –

+0

それは実際にGoogleドキュメントのエラーでもあります:-) –

+3

@MorganAllen:いいえ、GoogleドキュメントはPython 3ではなくPython 2用に書かれています。それらのバージョン間の違い。 Googleは、これをより明示的にしてPython 3の例を提供する可能性がありますが、現在のコードを間違ったものにすることはありません。 –

1

%は、printステートメント/関数ではなく文字列で処理されます。

# wrong 
print ('thread id: %s - number of messages in this thread: %d') % (thread['id'], len(messages)) 

# right 
print ('thread id: %s - number of messages in this thread: %d' % (thread['id'], len(messages))) 

実際はそれよりもう少し複雑です。 Python 2では、printは1つ以上の式を取り、それらを標準出力に出力するステートメントでした。 print ("hi")のようなステートメントは、printキーワードの後に​​カッコで囲まれた式が続きます。つまり、print ("%s") % ("hi",)は、printとして解析され、("%s") % ("hi,)の式が続き、これは"hi"と評価され、印刷されます。

は、Python 3では、printは、パーサが今%演算子とその2つの引数を、関数呼び出しprint("%s")とタプル("hi",)を含む、print ("%s") % ("hi",) 1として式を認識するという意味、機能になりました。 printNoneを返すので、%は、最初の引数がNoneであることをサポートしていないとPythonは苦情を言います。

6

Googleからのこの例は、Python 2用に書かれていますが、Python 3またはfrom __future__ import print_functionで使用しています。具体的に


:少なくともそれはexcept errors.HttpError, errorに変更された最後のprintの文とexcept Exception, e、から明らかであるけれどもGoogleがそのドキュメントの例ではPythonの時代遅れのバージョンを使用していることは非常に残念なことです

print ('thread id: %s - number of messages ' 
     'in this thread: %d') % (thread['id'], len(messages)) 

は、Python 2のために書かれた、および

print(('thread id: %s - number of messages ' 
     'in this thread: %d') % (thread['id'], len(messages))) 

01に変更されなければなりません

(print('something')) % (something, something) 

即ちオペレータ%printの戻り値とタプル(thread['id'], len(messages))に適用されるステートメントのようなものが解析されるように、パイソン3、print

は、Noneを返す関数です。

しかし、printが声明だったPython 2では、printキーワードの後のすべてが最初に評価されて印刷されていました。これはPython 2に、それは全部の周りに余分な括弧を追加

print (('something') % (something, something)) 

として解析され、文はそれらの余分括弧を持っていなかった場合、それは、Python 3


で正常に動作しますあなたはSyntaxError: Missing parentheses in call to 'print'” mean in Python?"を持っていて、すぐに明らかなエラーになっていました。

+1

あなたの最後の段落は、コードがこの点の近くに来る前に、プログラム内の他の 'print'ステートメントがSyntaxErrorを生成していないという興味深い質問を残します。 – Duncan

+0

おそらくユーザーはそれを得て、それを修正する方法を知っていたでしょう。 –

+0

@Duncanの固定言葉:d –

関連する問題