<commit_before>#getLegislators pulls basic info for legislators by state
	#url ='http://www.opensecrets.org/api/?method=getLegislators&id=NJ&apikey=__32cabcec9c4b9305dc3b94acd5109c9e__'
	#above is provided by website. a JSON url code. This is for reference now. Repeatable URL is below.

	#this specific call didn't have an example of what a space (" ") looks like
import urllib.request
import json
import pprint
import csv

printer = pprint.PrettyPrinter(indent=4)
OS_API = '<KEY>'
state_List = ['AL']
# , 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DC', 'DE', 'FL', 'GA', 
#           'HI', 'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MD', 
#           'MA', 'MI', 'MN', 'MS', 'MO', 'MT', 'NE', 'NV', 'NH', 'NJ', 
#           'NM', 'NY', 'NC', 'ND', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 
#           'SD', 'TN', 'TX', 'UT', 'VT', 'VA', 'WA', 'WV', 'WI', 'WY']

def OS_Search(item):
	api_key = OS_API
	url_Base = 'https://www.opensecrets.org/api/?method=getLegislators'
# state = 'NJ'
	state = item
	output = 'json'
	final_url = url_Base + "&id=" + state + "&apikey=" + api_key + '&output=' + output
	# I am trying this in the loop, but I don't think it is necessary or advised.
	# Andrew wrote the below code. It is a module that can be looked into @ https://docs.python.org/3/library/urllib.request.html
	req = urllib.request.Request(final_url, headers={'User-Agent': 'Mozilla/5.0'})
		# Thank god we have to pretend to be a browser????? This line is needed because OS doesn't like Python 3 calls when they are not from a browser.
	return_value = urllib.request.urlopen(req)
	print(return_value.status) 
		# This line specifically gives us a status code for how the pull went
	body = return_value.read()
	decoded_body = body.decode('utf-8')
	json_data = json.loads(decoded_body)

	printer.pprint(json_data)
# printer.pprint(json_data.keys())
# printer.pprint(json_data['response'].keys())
# printer.pprint(json_data['response']['legislator'].keys()) 
	# this will let you check what the keys are in the data field names. 
	# might have to repeat several times, they are nested
	with open('C:\Programming\OSAPI\\test.csv', "w") as csvfile:
		writer = csv.writer(csvfile, delimiter=',')
		for line in json_data:
			writer.writerow(line)

all_Data = []
for item in state_List:
	all_Data.append([item, OS_Search(item)])
<commit_msg>The recommend for json to csv.<commit_after>#getLegislators pulls basic info for legislators by state
	#url ='http://www.opensecrets.org/api/?method=getLegislators&id=NJ&apikey=__32cabcec9c4b9305dc3b94acd5109c9e__'
	#above is provided by website. a JSON url code. This is for reference now. Repeatable URL is below.

	#this specific call didn't have an example of what a space (" ") looks like
import urllib.request
import json
import pprint
import csv

printer = pprint.PrettyPrinter(indent=4)
OS_API = '<KEY>'
state_List = ['AL']
# , 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DC', 'DE', 'FL', 'GA', 
#           'HI', 'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MD', 
#           'MA', 'MI', 'MN', 'MS', 'MO', 'MT', 'NE', 'NV', 'NH', 'NJ', 
#           'NM', 'NY', 'NC', 'ND', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 
#           'SD', 'TN', 'TX', 'UT', 'VT', 'VA', 'WA', 'WV', 'WI', 'WY']

def OS_Search(item):
	api_key = OS_API
	url_Base = 'https://www.opensecrets.org/api/?method=getLegislators'
# state = 'NJ'
	state = item
	output = 'json'
	final_url = url_Base + "&id=" + state + "&apikey=" + api_key + '&output=' + output
	# I am trying this in the loop, but I don't think it is necessary or advised.
	# Andrew wrote the below code. It is a module that can be looked into @ https://docs.python.org/3/library/urllib.request.html
	req = urllib.request.Request(final_url, headers={'User-Agent': 'Mozilla/5.0'})
		# Thank god we have to pretend to be a browser????? This line is needed because OS doesn't like Python 3 calls when they are not from a browser.
	return_value = urllib.request.urlopen(req)
	print(return_value.status) 
		# This line specifically gives us a status code for how the pull went
	body = return_value.read()
	decoded_body = body.decode('utf-8')
	json_data = json.loads(decoded_body)

	printer.pprint(json_data)
# printer.pprint(json_data.keys())
# printer.pprint(json_data['response'].keys())
# printer.pprint(json_data['response']['legislator'].keys()) 
	# this will let you check what the keys are in the data field names. 
	# might have to repeat several times, they are nested
	with open('C:\Programming\OSAPI\\test.csv', "w") as csvfile:
		writer = csv.writer(csvfile, delimiter=',')
		for line in json_data:
			writer.writerow(line)

# replace with below. it will most likely require editing. this is original from andrew
# 	# f = open('mycsvfile.csv','wb')
	# w = csv.DictWriter(f,my_dict.keys())
	# w.writerows(my_dict)
	# f.close()


all_Data = []
for item in state_List:
	all_Data.append([item, OS_Search(item)])

{'fraction_non_alphanumeric': 0.16735028712059064, 'fraction_numerical': 0.009023789991796555, 'mean_word_length': 4.244086021505376, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 4, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6373667', 'n_tokens_mistral': 1833, 'n_tokens_neox': 1755, 'n_words': 539}