将JSON元素写入csv文件python

时间:2015-10-23 14:05:39

标签: python arrays json csv object

我刚创建了一个python程序,用于搜索google网站管理员API以检查target_site是否适合移动设备&然后基于响应它提取某些Json元素。它还捕获本地文件夹

上的屏幕截图

脚本工作正常,但是当我尝试将这些json对象写入csv文件时,它无法正常工作。

这是我的代码: -

import requests, json, string, random, time
import csv
from base64 import decodestring
from random import randint

#links = open(r'D:\\Carlos\\Links.txt')
links = ['https://www.googleapis.com/pagespeedonline/v3beta1/mobileReady?key=AIzaSyDkEX-f1JNLQLC164SZaobALqFv4PHV-kA&screenshot=true&snapshots=true&locale=en_US&url=https://www.economicalinsurance.com/en/&strategy=mobile&filter_third_party_resources=false',
         'https://www.googleapis.com/pagespeedonline/v3beta1/mobileReady?key=AIzaSyDkEX-f1JNLQLC164SZaobALqFv4PHV-kA&screenshot=true&snapshots=true&locale=en_US&url=http://www.volkswagen-me.com/en-vwme/service/protection/motor-insurance.html&strategy=mobile&filter_third_party_resources=false']

def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
    return ''.join(random.choice(chars) for _ in range(size))

i = 12

def get_data(each):
    try:
        r = requests.get(each)
    except:
        pass
    #time.sleep(randint(1, 3))
    try:    
        json_data = json.loads(r.text)
    except:
        pass
    try:
        score = json_data['ruleGroups']['USABILITY']['score'];score=int(score)
    except:
        pass
    try:
        Pass = json_data['ruleGroups']['USABILITY']['pass'];Pass=str(Pass)
    except:
        pass
    try:
        ConfigureViewport = json_data['formattedResults']['ruleResults']['ConfigureViewport']['localizedRuleName'];ConfigureViewport=str(ConfigureViewport)
    except:
        pass
    try:
        UseLegibleFontSizes = json_data['formattedResults']['ruleResults']['UseLegibleFontSizes']['localizedRuleName'];UseLegibleFontSizes=str(UseLegibleFontSizes)
    except:
        pass
    try:
        AvoidPlugins = json_data['formattedResults']['ruleResults']['AvoidPlugins']['localizedRuleName'];AvoidPlugins=str(AvoidPlugins)
    except:
        pass
    try:
        SizeContentToViewport = json_data['formattedResults']['ruleResults']['SizeContentToViewport']['localizedRuleName'];SizeContentToViewport=str(SizeContentToViewport)
    except:
        pass
    try:
        SizeTapTargetsAppropriately = json_data['formattedResults']['ruleResults']['SizeTapTargetsAppropriately']['localizedRuleName'];SizeTapTargetsAppropriately=str(SizeTapTargetsAppropriately)
    except:
        pass
    try:
        AvoidInterstitials = json_data['formattedResults']['ruleResults']['AvoidInterstitials']['localizedRuleName'];AvoidInterstitials=str(AvoidInterstitials)
    except:
        pass
    try:
        image_link = json_data['screenshot']['data']; image_link = image_link.replace("_", "/").replace("-","+")
    except:
        pass
    #try:
    id_generator_name = "".join( [random.choice(string.letters) for i in xrange(15)] )+'.jpeg'
    #except:
       # pass
    #try:
    fh = open(id_generator_name, "wb")
    #except:
    #    pass
    try:
        fh.write(str(image_link).decode('base64'))
        time.sleep(1)
    except:
        pass
    try:
        fh.close()
    except:
        pass
    try:
        error_code = json_data['error']['message'];error_code=str(error_code)
    except:
        pass
    try:
        print each, score, Pass, ConfigureViewport, UseLegibleFontSizes, AvoidPlugins, SizeContentToViewport, SizeTapTargetsAppropriately, AvoidInterstitials, error_code
    except:
        pass
    try:
        writer.writerow({'each':each, 'score':score, 'Pass':Pass, 'ConfigureViewport':ConfigureViewport,
                         'UseLegibleFontSizes':UseLegibleFontSizes, 'AvoidPlugins':AvoidPlugins,
                         'SizeContentToViewport':SizeContentToViewport,'SizeTapTargetsAppropriately':SizeTapTargetsAppropriately,
                         'AvoidInterstitials':AvoidInterstitials, 'error_code':error_code,'imagename':id_generator_name})
    except:
        pass

#path to the csv file
with open("D:\Carlos\Data_file\output.csv", "ab")as export:
    fieldnames = ['each', 'score', 'Pass', 'ConfigureViewport', 'UseLegibleFontSizes', 'AvoidPlugins', 'SizeContentToViewport',
                  'SizeTapTargetsAppropriately', 'AvoidInterstitials', 'error_code','imagename']
    writer = csv.DictWriter(export, fieldnames=fieldnames)
    writer.writeheader()
    for each in links:
    #try:
        get_data(each)
    #except:
    #  pass

请告知如何写csv?或者代码中哪里出错?

1 个答案:

答案 0 :(得分:2)

我喜欢使用Pandas数据帧,但如果你不使用Pandas,它可能会有点过分。 Pandas数据帧也非常适合分析和比较。

您可以将JSON放入数据框中,然后将数据框输出到CSV文件。

import pandas as pd

df = pd.read_json('path/to/json/file')
df.to_csv('filename.csv')

请注意,只有当您的JSON具有一个级别并且可能是一个csv时,它才是这么简单。否则,您需要将JSON读入dict,导航到适当的级别,然后将其读入数据帧。

http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_json.html http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_csv.html

http://pandas.pydata.org/