■やりたいこと
big queryのテーブルをqueryメソッドを使い操作したい
■やったこと
こちらのURLのスクリプトをそのまま実行
https://googleapis.dev/python/bigquery/latest/usage/queries.html
■起きたこと
最初は問題なく動いたが、何故か途中でエラーがでてしまうようになった
エラー内容:TypeError: string indices must be integers
from google.cloud import bigquery
# Construct a BigQuery client object.
client = bigquery.Client()
query = """
SELECT name, SUM(number) as total_people
FROM `bigquery-public-data.usa_names.usa_1910_2013`
WHERE state = 'TX'
GROUP BY name, state
ORDER BY total_people DESC
LIMIT 20
"""
query_job = client.query(query) # Make an API request.
print("The query data:")
for row in query_job:
# Row values can be accessed by field name or index.
print("name={}, count={}".format(row[0], row["total_people"]))
最初は上記のコードをそのまま実行し、動作確認をして動いていた気がするのですが
途中でエラーがでてしまいうまくいきません。
どなたかおわかりになる方いましたら、ご教示いただけますと幸いです。
print("name={}, count={}".format(row[0], row["total_people"]))
とか言う風にフォーマットした内容では無く、print(row)
やprint(row[0])
とかprint(dir(row))
やprint(dir(row[0]))
とかでフォーマットしないで表示することは出来るのでしょうか? あるいは、仕様が変わったか何かして、row
が2次元配列で返される場合があるとか? その場合、print("name={}, count={}".format(row[0][0], row[0]["total_people"]))
という風にして表示出来るか試してみてはどうでしょう?