I have a CSV with UTF-16 encoding I am trying to upload to streamlit.
I am getting an error for line 18 where I defined UTF-16 encoding so there’s not much more I can do. Here is the line:
with open(file1, newline=‘’, encoding=‘utf-16’) as csvfile:
Any ideas? I can open the same file in notebook so I suppose this is streamlit related problem.
Are you saying you have some file1 = st.file_uploader("Choose a file") and are using
with open(file1, newline=‘’, encoding=‘utf-16’) as csvfile:?
If so, note that the file uploader widget explictly returns the file in ByteIO. The with open(...) arugment is looking for a file path/name instead of a file object.
Making an adjustment to the example shown in the documentation, try:
import streamlit as st
from io import StringIO
import pandas as pd
file = st.file_uploader("Choose a file")
if file != None:
bytes_data = file.getvalue()
string_data = StringIO(bytes_data.decode("utf-16"))
data = pd.read_csv(string_data)
data
Note that something like pd.read_csv accepts a “str, path object or file-like object” so you have some flexibility. I’m not sure what you were going to ultimately do in your with open(..) so feel free to clarify further if needed.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.