2017-08-17 1 views
1

Windowsマシンで実行すると動作するコードがいくつかありますが、Google ComputeEngine VMのUbuntuで実行すると、次のエラーが発生します。API応答チャンクを取得します。エラー:データバイトではない文字列(ubuntu)

Traceback (most recent call last): File "firehose_get.py", line 43, in print(json.dumps(json.loads(line),indent=2)) File "/home/stuartkirkup/anaconda3/lib/python3.5/json/init.py", line 312, in loads s.class.name)) TypeError: the JSON object must be str, not 'bytes'

これは、Windowsで正常に動作するコードとまったく同じです。私はかなり読んだことがあり、エンコードの問題のように見えます。コード内のコメントアウトされたセクションのいくつかからわかるように、私はエンコードを変更するいくつかの方法を試しましたが、喜びはありません。私はいろいろなことを試しましたが、デバッグ方法を理解することはできません。私はかなり新しいPythonです。

私はAnacondaを使用しています。 「木、2017年8月17日午前16時53:「日付を」「チャンク」:ここで

は、私はそれのバイト

{「転送エンコードは」なぜであると信じて、それがチャンクのデータを示すストリームヘッダであります: 'gzip'}

コード・ファイル - firehose_requests.py(wi(i))、「コンテンツ・タイプ」:「コンテンツ・タイプ」:「アプリケーション/ json」、「x-se rver」:「db220」、「コンテンツ・エンコーディング」: ####に置き換え番目のAPIキーインフォア)

import requests 

MAX_REDIRECTS = 1000 

def get(url, **kwargs): 
    kwargs.setdefault('allow_redirects', False) 
    for i in range(0, MAX_REDIRECTS): 
     response = requests.get(url, **kwargs) 
     #response.encoding = 'utf-8' 
     print ("test") 
     print (response.headers) 
     if response.status_code == requests.codes.moved or \ 
      response.status_code == requests.codes.found: 
      if 'Location' in response.headers: 
       url = response.headers['Location'] 
       content_type_header = response.headers.get('content_type') 
       print (content_type_header) 

       continue 
      else: 
       print ("Error when reading the Location field from HTTP headers") 
     return response 

コードファイル - firehose_get.py

import json 
import requests 
from time import sleep 
import argparse 
#import ConfigParser 
import firehose_requests 
from requests.auth import HTTPBasicAuth 
# Make it work for Python 2+3 and with Unicode 
import io 

try: 
    to_unicode = unicode 
except NameError: 
    to_unicode = str 

#request a token from Adobe 
request_access_token = requests.post('https://api.omniture.com/token', data={'grant_type':'client_credentials'}, auth=HTTPBasicAuth('##############-livestream-poc','488##############1')).json() 
#print(request_access_token) 

#grab the token from the JSON returned 
access_token = request_access_token["access_token"] 
print(access_token) 

url = 'https://livestream.adobe.net/api/1/stream/eecoukvanilla-##############' 

sleep_sec=0 
rec_count=10 
bearer = "Bearer " + access_token 
headers = {"Authorization": bearer,"accept-encoding":"gzip,deflate"} 
r = firehose_requests.get(url, stream=True, headers=headers) 

#open empty file 
with open('output_file2.txt', 'w') as outfile: 
    print('', file=outfile) 

#Read the Stream 
if r.status_code == requests.codes.ok: 
    count = 0 
    for line in r.iter_lines(): 
     if line: 
      #write to screen 
      print ("\r\n") 
      print(json.dumps(json.loads(line),indent=2)) 
      #append data to file 
      with open('output_file2.txt', 'a') as outfile: 
       print("\r\n", file=outfile) 
       print(json.dumps(json.loads(line),ensure_ascii = False),file=outfile) 

      #with io.open('output_file2.txt', 'w', encoding='utf8') as outfile: 
      # str_ = json.dumps(json.loads(line), 
      #      indent=4, sort_keys=True, 
      #      separators=(',', ': '), ensure_ascii=False) 
      # outfile.write(to_unicode(str_)) 



      #Break the loop if there are is a -n argument 
      if rec_count is not None: 
       count = count + 1 
       if count >= rec_count: 
        break 


      #How long to wait between writes   
      if sleep_sec is not None : 
       sleep(sleep_sec) 
else: 
    print ("There was a problem with the Request") 
    print ("Returned Status Code: " + str(r.status_code)) 

OKおかげ

答えて

1

は、私はそれを働きました。私は多くの人々も、このエラーを取得したが、何の解決策は掲載しないので、これは私がそれを

解析をした方法であり、この

json_parsed =のjson.loads(line.decode( "UTF-のようなJSONをデコード8" ))

全コード:

import json 
import requests 
from time import sleep 
import argparse 
#import ConfigParser 
import firehose_requests 
from requests.auth import HTTPBasicAuth 
# Make it work for Python 2+3 and with Unicode 
import io 

try: 
    to_unicode = unicode 
except NameError: 
    to_unicode = str 

#request a token from Adobe 
request_access_token = requests.post('https://api.omniture.com/token', data={'grant_type':'client_credentials'}, auth=HTTPBasicAuth('##########-livestream-poc','488################1')).json() 
#print(request_access_token) 

#grab the token from the JSON returned 
access_token = request_access_token["access_token"] 
print(access_token) 

url = 'https://livestream.adobe.net/api/1/stream/##################' 

sleep_sec=0 
rec_count=10 
bearer = "Bearer " + access_token 
headers = {"Authorization": bearer,"accept-encoding":"gzip,deflate"} 
r = firehose_requests.get(url, stream=True, headers=headers,) 

#open empty file 
with open('output_file.txt', 'w') as outfile: 
    print('', file=outfile) 


#Read the Stream 
if r.status_code == requests.codes.ok: 
    count = 0 
    for line in r.iter_lines(): 
     if line: 

      #parse and decode the JSON   
      json_parsed = json.loads(line.decode("utf-8")) 

      #write to screen 
      #print (str(json_parsed)) 
      #append data to file 
      with open('output_file.txt', 'a') as outfile: 

       #write to file 
       print(json_parsed,file=outfile) 


      #Break the loop if there are is a -n argument 
      if rec_count is not None: 
       count = count + 1 
       if count >= rec_count: 
        break 


      #How long to wait between writes   
      if sleep_sec is not None : 
       sleep(sleep_sec) 
else: 
    print ("There was a problem with the Request") 
    print ("Returned Status Code: " + str(r.status_code)) 
関連する問題