Issue
I am uploading a file to colab using:
from google.colab import files
uploaded = files.upload()
Then i am trying to read all lines in the uploaded file:
for line in uploaded:
print(line)
But I am getting the file name text.txt
instead of its lines.
How can I proper read it?
Thanks
Solution
The uploaded
object is a dictionary containing the name of the file and the contents for each uploaded file.
Here's some code that loops through all uploaded files and for each one attempts to read the contents into a list of lines which it then prints.
from google.colab import files
uploaded = files.upload()
for filename in uploaded.keys(): # get the name of each uploaded file
print(filename)
lines = uploaded[filename].splitlines() # split the contents into lines
for line in lines: # iterate over all lines
print(line.decode('utf-8')) # print the line (it needs to be decoded)
Answered By - Andrew Chisholm
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.