Cannot Export API call data to CSV - python

I am trying to export the json received from an API call to csv using the code below but getting the error messages:
import requests
import csv
import json
r='http://openapi.seoul.go.kr:8088/504949776872656935396c46496663/json/airPolutionMeasuring1Hour/1/50/'
response=requests.get(r)
output = response.text
jsonresponse=json.loads(output)
with open ('data_02.csv', 'w', newline ='') as csvfile:
fieldnames=['DATA_DT', 'LOC_CODE', 'ITEM_CODE', 'DATA_VALUE', 'DATA_STATE', 'DATA_NOVER', 'DATA_ROVER', "REGIST_DT"]
writer=csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for row in jsonresponse:
writer.writerow(row)
#print(output)
The error message:
Traceback (most recent call last): File
"/Users/xxx/PycharmProjects/api_request/export_csv_Test02.py", line
16, in
writer.writerow(row) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/csv.py",
line 154, in writerow
return self.writer.writerow(self._dict_to_list(rowdict)) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/csv.py",
line 147, in _dict_to_list
wrong_fields = rowdict.keys() - self.fieldnames AttributeError: 'str' object has no attribute 'keys'

Instead of
for row in jsonresponse:
writer.writerow(row)
Use:
writer.writerows(jsonresponse)

import requests
import csv
import json
r='http://openapi.seoul.go.kr:8088/504949776872656935396c46496663/json/airPolutionMeasuring1Hour/1/50/'
response=requests.get(r)
output = response.text
jsonresponse=json.loads(output)
with open ('data_02.csv', 'w', newline ='') as csvfile:
fieldnames=['DATA_DT', 'LOC_CODE', 'ITEM_CODE', 'DATA_VALUE', 'DATA_STATE', 'DATA_NOVER', 'DATA_ROVER', "REGIST_DT"]
writer=csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(jsonresponse['airPolutionMeasuring1Hour']['row'])
# print(output)

Related

Encode a column in CSV to Base64

I'll preface by saying I'm a novice with Python, but I'm trying to encode a single column from a CSV to Base64 and write to another CSV. The file has 3 columns (consumer_id, sms_number, email_address) and I only want to encode the 'consumer_id'. Here is what I have as of now:
import base64
with open('File1.csv') as csvfile:
with open('File2.csv', 'w') as newfile:
reader = csv.DictReader(csvfile)
for i, r in enumerate(reader):
# writing csv headers
if i == 0:
newfile.write(','.join(r) + '\n')
# convert 'ID' column to Base64
r['consumer_id'] = base64.b64decode(parse.unquote(row['consumer_id']))
# writing the new row to the file
newfile.write(','.join(r.values()) + '\n')
The error I get is
Traceback (most recent call last):
File "c:\script.py", line 93, in <module>
r['consumer_id'] = base64.b64decode(parse.unquote(row['consumer_id']))
NameError: name 'parse' is not defined. Did you mean: 'vars'?
There are a few errors:
you did not import urllib, which is the reson for the error message that you got: -> from urllib import parse
you want to encode, not decode: -> base64.b64encode
you're also missing the import csv
row is not defined: -> change r to row
Full code:
import base64
import csv
from urllib import parse
with open('C:/temp/File1.csv') as csvfile:
with open('C:/temp/File2.csv', 'w') as newfile:
reader = csv.DictReader(csvfile)
for i, row in enumerate(reader):
# writing csv headers
if i == 0:
newfile.write(','.join(row) + '\n')
# convert 'ID' column to Base64
row['consumer_id'] = base64.b64encode(parse.unquote(row['consumer_id']).encode()).decode()
# writing the new row to the file
newfile.write(','.join(row.values()) + '\n')

OSError: [Errno 22] Invalid argument while opening a file

I am currently working on an application which translates a input file row by row and writes the translated row into a output file but I get the following error when running my script: OSError: [Errno 22] Invalid argument: '{parent_id:opynw1, body: my text'
Full error message:
Traceback (most recent call last):
File "test.py", line 12, in <module>
column1= GoogleTranslator(source='german', target='english').translate_file(row)
File "C:\Users\supre\anaconda3\envs\test\lib\site-packages\deep_translator\google_trans.py", line 136, in translate_file
raise e
File "C:\Users\supre\anaconda3\envs\test\lib\site-packages\deep_translator\google_trans.py", line 132, in translate_file
with open(path) as f:
OSError: [Errno 22] Invalid argument: '{parent_id:opynw1, body: my text'
Content of my input text file:
{parent_id:opynw1, body: my text}
{parent_id:h68dhu3, body: my text}
{parent_id:opynw1, body: my text}
My code:
from deep_translator import GoogleTranslator
output_file = open('./test.txt', 'w',encoding='utf-8')
reader = open('./data.txt', 'r', encoding='utf-8')
for row in reader:
column1= GoogleTranslator(source='english', target='german').translate_file(row)
output_text = str(column1)
output_file.write(output_text + '\n')
output_file.close()
Is there something wrong with my script? If so I would be glad if someone could tell me what I've made wrong and help me to solve this problem:)
Thank's for every help and suggestion in advance:)
Use translate() rather than translate_file(), since the line is not a filename.
You also need to use ast.literal_eval() to parse it into a dictionary.
from deep_translator import GoogleTranslator
import ast
with open('./test.txt', 'w',encoding='utf-8') as output_file, open('./data.txt', 'r', encoding='utf-8') as reader:
for row in reader:
d = ast.literal_eval(row)
column1= GoogleTranslator(source='english', target='german').translate(d['body'])
output_text = str(column1)
output_file.write(output_text + '\n')

Why do I receive an error stating coercing to unicode in Python?

I am trying to write a code that will fetch an api from my csv as input:
from pyzillow.pyzillow import ZillowWrapper, GetDeepSearchResults, GetUpdatedPropertyDetails
def get_zillowinfo(address,zipcode):
zillow_data = ZillowWrapper('X1-ZWz17seirkzuh7_93aho')
deep_search_response = zillow_data.get_deep_search_results(address,zipcode)
result1 = GetDeepSearchResults(deep_search_response) #get zillowid from address
updated_property_details_response = zillow_data.get_updated_property_details(result1.zillow_id)
result2 = GetUpdatedPropertyDetails(updated_property_details_response) # get detail property info
result = result2.home_info
return result
print get_zillowinfo('73 main blvd','01545')
#2
import csv
with open(r'C:\Users\bca\Desktop\Pricing Study-Zillow\sample4.csv', 'r') as csvfile:
spamreader = csv.reader(csvfile,delimiter=',')
next(spamreader)
for row in spamreader:
print row
#3
import csv
with open(r'C:\Users\bca\Desktop\Pricing Study-Zillow\sample4.csv', 'r') as csvfile:
spamreader = csv.reader(csvfile,delimiter=',')
next(spamreader)
for row in spamreader:
print get_zillowinfo(row[0],row[1])
When i do step #3, I get an error:
Traceback (most recent call last):
File "C:\Users\bca\AppData\Local\Continuum\anaconda2\lib\site-packages\IPython\core\interactiveshell.py", line 2895, in run_code
self.showtraceback()
File "C:\Users\bca\AppData\Local\Continuum\anaconda2\lib\site-packages\IPython\core\interactiveshell.py", line 1828, in showtraceback
self._showtraceback(etype, value, stb)
File "C:\Users\bca\AppData\Local\Continuum\anaconda2\lib\site-packages\ipykernel\zmqshell.py", line 547, in _showtraceback
u'evalue' : py3compat.safe_unicode(evalue),
File "C:\Users\bca\AppData\Local\Continuum\anaconda2\lib\site-packages\ipython_genutils\py3compat.py", line 65, in safe_unicode
return unicode_type(e)
TypeError: coercing to Unicode: need string or buffer, dict found
Why does this happen? is it because my characters are not strings? how do I change it to string in that case for all my data?
This is a repex of my dataset:
What do I need to change in my code to avoid that type error?

I need my csv out put to use commas instead of semicolons so I can insert it into my sqlite DB

Right now my nmap csv is putting semicolons in the file which I need to change to commas.
nmap scan
import first
import csv
import nmap
csvFilePath = "nmapscan1.csv"
ipAddress = first.ipAddress
port = first.port
#nmap scan using user input varibles
nm = nmap.PortScanner()
nm.scan(ipAddress,port)
csv = nm.csv()
print(csv)
#writing to csv file
with open(csvFilePath, "w") as csvFile:
csvFile.write(csv)
#changes ; to , for database use
with open(r"nmapscan1.csv") as in_file, open(r"nmapscan.csv", 'w') as
out_file:
semicolonin = csv.reader(in_file, delimiter=';')
commaout = csv.writer(out_file, delimiter=',')
for row in semicolonin:
commaout.writerow(row)
error I get in Ubuntu terminal
Traceback (most recent call last):
File "second.py", line 23, in <module>
semicolonin = csv.reader(in_file, delimiter=';')
AttributeError: '_io.TextIOWrapper' object has no attribute 'reader'
Set a different name to csv on csv = nm.csv(). It is overwriting your csv on import csv.
Edited*
This part:
csv_data = nm.csv()
print(csv_data)
#writing to csv file
with open(csvFilePath, "w") as csvFile:
csvFile.write(csv_data)

how to make a copy from csv file to edit it

when i'm trying to make a copy from csv file to edit it away of the original
then I apply the effects to the original
import csv
import shutil
from tempfile import NamedTemporaryFile
filename = "data1.csv"
temp_file = NamedTemporaryFile(delete=False)
print(temp_file.name)
with open(filename, "r",encoding='utf8') as csvfile, temp_file:
reader = csv.DictReader(csvfile)
fieldnames = ["id", "name", "email", "sent"]
writer = csv.DictWriter(temp_file, fieldnames=fieldnames)
# writer.writeheader()
for row in reader:
writer.writerow({
"id":row["id"],
"name":row["name"],
"email":row["email"],
"sent":""
})
I get this error :/
C:\Users\Arafat\AppData\Local\Temp\tmpwgkcslas
Traceback (most recent call last):
File "C:\Users\Arafat\Desktop\30dpython\hungry_data.py", line 49, in <module>
"sent":""
File "C:\Users\Arafat\AppData\Local\Programs\Python\Python36-32\lib\csv.py", line 155, in writerow
return self.writer.writerow(self._dict_to_list(rowdict))
File "C:\Users\Arafat\AppData\Local\Programs\Python\Python36-32\lib\tempfile.py", line 483, in func_wrapper
return func(*args, **kwargs)
TypeError: a bytes-like object is required, not 'str'
The error is the result of your temp_file being opened in binary mode rather than text mode (the default is w+b). Change it to:
temp_file = NamedTemporaryFile(mode='w', encoding='utf8', delete=False)
(the encoding is not strictly necessary, but since you're specifying it on the input, makes sense to specify it on the output).
See https://docs.python.org/3/library/tempfile.html

Categories

Resources