how to make a copy from csv file to edit it - python

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

Related

Cannot Export API call data to CSV

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)

Search for list values in a csv file python

There is a csv file, there is a unique id column and a column with a date. The task of this section of the code is that you need to scan the lines using the id key, find the id in the line, write the line to a new file called id. Faced with the problem that the interpreter returns an error by id. Although everything is logical and correct. Where did I go wrong?
id, Numder, Date
123456, 89654535556, 25.11.2021 15:35:00
321654, 96554412255, 23.11.2021 18:50:00
524163, 38095224444, 18.11.2021 13:30:00
from csv import DictReader
from csv import DictWriter
from os.path import isfile
def export_csv(user_id, master_csv, fieldnames, key_id, extension=".csv"):
filename = user_id + extension
file_exists = isfile(filename)
with open(file=master_csv) as in_file, open(
file=filename, mode="a", newline=""
) as out_file:
# Create reading and writing objects
csv_reader = DictReader(in_file)
csv_writer = DictWriter(out_file, fieldnames=fieldnames)
# Only write header once
if not file_exists:
csv_writer.writeheader()
# Go through lines and match ids
for line in csv_reader:
if line[key_id] == user_id:
# Modify line and append to file
line = {k: v.strip() for k, v in line.items() if k in fieldnames}
csv_writer.writerow(line)
export_csv(
user_id="512863",
master_csv="master.csv",
fieldnames=["Number", "Date"],
key_id="id")
Traceback (most recent call last):
File "C:/Users/sedei/PycharmProjects/convector/1.py", line 58, in
export_csv(
File "C:/Users/sedei/PycharmProjects/convector/1.py", line 52, in export_csv
if line[key_id] == user_id:
KeyError: 'id'

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?

Iterator should return strings, not list error with csv.reader

This code shows the error in the title.. Help me out plz.I wrote the following code to write to a csv file reading the input from another file but this shows the error.When i change "r" to "rb" then it shows "iterator should return byte" error.
error->Traceback (most recent call last):
File "C:/Users/abhirav.sati/Downloads/salesdata.py", line 34, in
csv_reader(csvReader,path)
File "C:/Users/abhirav.sati/Downloads/salesdata.py", line 11, in csv_reader
for row in read:
_csv.Error: iterator should return strings, not list (did you open the file in text mode?)
import csv
def csv_reader(fileobj,path):
read=csv.reader(fileobj,delimiter=',')
with open(path, "wt") as csv_file:
write=csv.writer(csv_file, delimiter=',')
i=1
for row in read:
if(i==1):
write.writerow(",".join(row))
i=2
continue
if(row[3]=="Trade"):
continue
else:
if(row[6]==NULL):
r=[row[0],row[0],"A,",row[8],row[9],row[0]]
#r=row[0]+row[0]+"A,"+row[8]+row[9]+row[0]
write.writerow(r)
else:
r=[row[0],row[0],"B,",row[6],row[7],row[0]]
#r=row[0]+row[0]+"A,"+row[8]+row[9]+row[0]
write.writerow(r)
if __name__ == "__main__":
path="sales.csv"
csv_path = "FlowEdge-TRTH-Time_Sales.csv"
f_obj = open(csv_path, "r")
data=csv.reader((line.replace('\0','') for line in f_obj), delimiter=",")
csv_reader(data,path)
Your code is applying csv.reader on another csv.reader object. Look carefully at your __main__ and csv_reader function to make sure you understand why this is the case.
A better solution is to use with open... with multiple files:
with open(path, 'wt') as f1, open('csv_path', 'r') as f2:
write = csv.writer(f1, delimiter=',')
read = csv.reader(f2, delimiter=',')

someone help me to fix IOError?

This is the function which make an attempt to update the content:
def write_csv(self, file_path, fields, rows):
with open(file_path, 'wb') as csvFile:
writer = csv.DictWriter(csvFile, fieldnames=fields)
writer.writerow(dict(zip(fields, fields)))
for row in rows:
writer.writerow(row)
The error which I'm getting is as follows:
C:\sample_data_set.csv
Initial Records : 19
Removed : 3
Traceback (most recent call last):
File "C:/Python_project/amit_test.py", line 79, in <module>
v.main()
File "C:/Python_project/amit_test.py", line 73, in main
self.write_csv('output/{0}'.format(os.path.basename(file_path)), fields, rows)
File "C:/Python_project/amit_test.py", line 41, in write_csv
with open(file_path, 'wb') as csvFile:
IOError: [Errno 2] No such file or directory: 'output/sample_data_set.csv'
I have checked that the path of sample_data_set.csv correct and does exist.
Complete code is available here https://gist.github.com/shashank136/f5557a1d0a42d1ae615a6e4c12b21ff7
file_path
maybe
file_path = r"C:\Python_project\output\sample_data_set.csv"
you can try!
Depends on your file_path, notes that it doesn't automatically create the intermediate directories when you opening a file
import os
def write_csv(self, file_path, fields, rows):
os.makedirs(os.path.dirname(file_path), exist_ok=True)
with open(file_path, 'wb') as csvFile:
writer = csv.DictWriter(csvFile, fieldnames=fields)
writer.writerow(dict(zip(fields, fields)))
for row in rows:
writer.writerow(row)

Categories

Resources