簡體   English   中英

類型錯誤:我的 python 腳本需要一個整數

[英]TypeError: an integer is required on my python script

我正在做一個python代碼:

  • 從 API 獲取數據
  • 解析數據
  • 構建查詢並將其發送到我的 PGadmin 數據庫

這是我的代碼:

   # -*- coding: utf-8 -*-
import requests
import json
import datetime
import os
import psycopg2
from urlparse import urlparse


try:
    conn = psycopg2.connect("dbname='tp_1' user='postgres' host='localhost' password='senha'")
except psycopg2.DatabaseError, ex:
    print 'I am unable to connect the database: ' + str(ex)

cur = conn.cursor()
url = "https://www.mercadobitcoin.net/api/BTC/day-summary/2013/6/20/"

moeda = url.split('/')[4]

response = requests.get(url)

print("---------------------")
data = response.text
print(data)
print("---------------------")

parsed = json.loads(data)

opening = parsed["opening"]
closing = parsed["closing"]
lowest = parsed["lowest"]
highest = parsed["highest"]
volume = parsed["volume"]
quantity = parsed["quantity"]
amount = parsed["amount"]
avg_price = parsed["avg_price"]
date = parsed["date"]

print(opening)
print(closing)
print(lowest)
print(highest)
print(volume)
print(quantity)
print(amount)
print(avg_price)
print(date)

SQL = "INSERT INTO day_summary (id_day, data_day, opening, closing, lowest, highest ,volume,quantity, amount, avg_price )  VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
data = (1,datetime.date(date), opening, closing, lowest, highest, volume, quantity, amount, avg_price)
cur.execute(SQL, data)
conn.commit()
cur.close()
conn.close()

這是我打印的輸出:

---------------------
{"date": "2013-06-20", "opening": 262.99999, "closing": 269.0, "lowest": 260.00002, "highest": 269.0, "volume": 7253.13363568, "quantity": 27.11390588, "amount": 28, "avg_price": 267.50604165}
---------------------
262.99999
269.0
260.00002
269.0
7253.13363568
27.11390588
28
267.50604165
2013-06-20

這是我的回溯:

Traceback (most recent call last):
  File "dados_api_day.py", line 56, in <module>
    data = (1,datetime.date(date), opening, closing, lowest, highest, volume, quantity, amount, avg_price)
TypeError: an integer is required

我無法解決問題。 我希望我能說得足夠清楚,所以你可以幫助我! 謝謝!

得到它的工作。 是日期造成的。 實際上,當我從 URL 解析數據時,日期變量是 UNICODE。 您需要做的是通過執行以下操作來轉換它:

your_new_date = datetime.datetime.strptime(date_that_you_want_to_convert, '%Y-%m-%d')

希望它會有所幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM