Error calling decode on UploadedFile type?

When using uploaded_file.decode('utf-8'), I encounter:

AttributeError: 'UploadedFile' object has no attribute 'decode'

The documentation example seems to suggest that one can call decode on an UploadedFile (API reference — Streamlit 0.74.1 documentation). Is that still the case? Or should one wrap it in a TextIOWrapper as suggested by the deprecation note (Version 0.64.0 • Deprecation warning for st.file_uploader decoding)?

Apologies for our docs typo! We have a PR out to fix that up so it should be resolved shortly.

Unfortunately we do not offer any shortcut to convert an UploadedFile to a string. It is a file-like object so you would first have to read the contents and then decode it in order to create a StringIO object.

stringio = StringIO(uploaded_file.getvalue().decode("utf-8"))

You could also wrap into a TextIOWrapper. TextIOWrapper can decode a buffer easily whereas StringIO takes in a string.

Ultimately it would depend on what type of object you would prefer to use in order to work with strings but both works!

1 Like