Han
Han

Reputation: 131

Open file as string instead of bytes in python

I am working on a project that reads a url which contains an ICS file (icalendar). Instead of reading it as a string it prints as bytes need some advice on this.

import requests
url = "http://ical.keele.ac.uk/index.php/ical/ical/15021113"
c = requests.get(url)
c.encoding = 'ISO-8859-1'
print(c.content)

Expected return

BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN
BEGIN:VEVENT

Actual return

b"BEGIN:VCALENDAR\rVERSION:2.0\rPRODID:-//hacksw/handcal//NONSGML v1.0//EN\rBEGIN:VEVENT\r

I have tried using the ics file directly and works without any problems but when I request from url it doesnt work. Thanks

Upvotes: 0

Views: 235

Answers (1)

handle
handle

Reputation: 6329

Delirious Lettuce is right, just use text:

http://docs.python-requests.org/en/master/user/quickstart/#response-content

import requests
url = "http://ical.keele.ac.uk/index.php/ical/ical/15021113"
c = requests.get(url)
#c.encoding = 'ISO-8859-1'
#print(c.content)
print(c.text[:10])

results in

BEGIN:VCAL

(3.6.1 32 bit windows)

Upvotes: 1

Related Questions