Write to a file (Python)

Uit De Vliegende Brigade
Versie door Jeroen Strompf (overleg | bijdragen) op 4 feb 2019 om 21:56
(wijz) ← Oudere versie | Huidige versie (wijz) | Nieuwere versie → (wijz)
Naar navigatie springen Naar zoeken springen
#! /usr/bin/python3
#
# Download Channable-XML-file - Strompf, Feb. 2019
#############################################################################
#
import urllib.request


#############################################################################
# Read
#############################################################################
#
url = 'https://www.example.com/fr-fr_channable.xml'
response = urllib.request.urlopen(url)
file_content_raw = response.read()      # a `bytes` object
file_content_decodes = file_content_raw.decode('utf-8') # a `str`; this step can't be used if data is binary


#############################################################################
# Write
#############################################################################
#
# testvar="Hello, world!"

f = open('testfile-02','w')
f.write(file_content_decodes)
f.close()

Bronnen