R
R
Ruslan Mordovanech2021-12-12 20:01:58
Python
Ruslan Mordovanech, 2021-12-12 20:01:58

Why do you overwrite when downloading a CSV file?

import requests

link = 'https://dsa.court.gov.ua/open_data_json.php?json=532'

response = requests.get(link).json()
urls = []
for item in response['Файли']:
    urls.append(list(item.values())[0])
    for url in urls:
        with requests.get(url, stream=True) as response:
            response.raise_for_status()

            with open("1data_sit.csv", "wb") as file:
                for chunk in response.iter_content(chunk_size=8192):
                    file.write(chunk)
                file.flush()

Everything shakes but it overwrites the final file. And I'm wondering, is it possible to put a filter when writing the final file to 'court_name'?
Tried "w" wrote an error.
file.write(chunk)
TypeError: write() argument must be str, not bytes
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dmshar, 2021-12-13
@Hery1

The "w" processing method is OVERWRITE the entire file. By calling with open("1data_sit.csv", "wb") as file inside the loop, each time you delete what was written earlier (at the previous iteration) in the file. If you want to append - use "a".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question