简体   繁体   中英

How to decode the file in Google colab using python?

this is the file that I have to run in google colab but the problem is I can no see the string. it is in encoding I have to decode that file so how can I do that.

This is the code for fetching file from the google drive.

import urllib.request
    response = urllib.request.urlopen('https://drive.google.com/file/d/1L4zhc1Vdpdy78t8CgZgA165siRtNaEsQ/view?usp=sharing')
    html = response.read()
    print(html)

This is the output of my file which i fetch from google drive.

b'<!DOCTYPE html><html><head><meta name="google" content="notranslate"><meta http-equiv="X-UA-Compatible" content="IE=edge;"><style nonce="x--bC4aRxWuHAUsRhwmqpQ">@font-face{font-family:\'Roboto\';font-style:italic;font-weight:400;src:url(//fonts.gstatic.com/s/roboto/v18/KFOkCnqEu92Fr1Mu51xIIzc.ttf)format(\'truetype\');}@font-face{font-family:\'Roboto\';font-style:normal;font-weight:300;src:url(//fonts.gstatic.com/s/roboto/v18/KFOlCnqEu92Fr1MmSU5fBBc9.ttf)format(\'truetype\');}@font-face{font-family:\'Roboto\';font-style:normal;font-weight:400;src:url(//fonts.gstatic.com/s/roboto/v18/KFOmCnqEu92Fr1Mu4mxP.ttf)format(\'truetype\');}@font-face{font-family:\'Roboto\';font-style:normal;font-weight:500;src:url(//fonts.gstatic.com/s/roboto/v18/KFOlCnqEu92Fr1MmEU9fBBc9.ttf)format(\'truetype\');}@font-face{font-family:\'Roboto\';font-style:normal;font-weight:700;src:url(//fonts.gstatic.com/s/roboto/v18/KFOlCnqEu92Fr1MmWUlfBBc9.ttf)format(\'truetype\');}</style><meta name="referrer" content="origin"><title>indian_histpry.txt - Google Drive</title><meta property="og:title" content="indian_histpry.txt"><meta property="og:type" content="article"><meta property="og:site_name" content="Google Docs"></script></body></html>'

How to Decode this encoded file using python?

Try this instead:

import urllib.request
response = urllib.request.urlopen('https://drive.google.com/u/0/uc?id=1L4zhc1Vdpdy78t8CgZgA165siRtNaEsQ&export=download')
html = response.read().decode('utf8')
print(html)

This works for me


import urllib.request
response = urllib.request.urlopen('https://drive.google.com/u/0/uc?id=1L4zhc1Vdpdy78t8CgZgA165siRtNaEsQ&export=download')
html = response.read()
decoded_text=html.decode('utf8')
print(decoded_text)
import urllib.request
response = urllib.request.urlopen('https://drive.google.com/file/d/1L4zhc1Vdpdy78t8CgZgA165siRtNaEsQ/view?usp=sharing')
html = response.read().decode(encoding='UTF-8',errors='strict')
print(html)

Update - This is tested and working

Install the PyDrive wrapper & import libraries.

!pip install -U -q PyDrive

import libraries

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials

Authenticate and create the PyDrive client.

auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

Download a file based on its file ID.

downloaded = drive.CreateFile({'id':'1L4zhc1Vdpdy78t8CgZgA165siRtNaEsQ'}) # replace the id with id of file you want to access
downloaded.GetContentFile('indian_histpry.txt')  

Read file content and print

with open('indian_histpry.txt', "r") as file1:
    FileContent = file1.readlines()

print(FileContent)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM