Unable to read files using standard file_uploader

I’m trying to import two xml files, convert them to dataframes and do some analysis. However, since these are xml’s I need to first convert them to json or excel to be able to create dataframes. With excel files, the file_uploader works directly, however, with xmls, I first need to open, read and then transform.

 import xmltodict
 import json
 file1 = st.file_uploader('File 1')
 file2= st.file_uploader('File 2')

 if st.button('Compare Files'):
         with open(file1 ,'r',encoding='utf-8') as in_file:
             xml = in_file.read()
             file1_data = json.loads(json.dumps(xmltodict.parse(xml)))

When I try the above snippet, the file is taken in as String.IO object and is not able to be read and throws the below error. Did anyone see any similar issue ? Know this is more of an SO post, but I thought putting it here would be more helpful.

1 Like

Hello @vijaysaimutyala, welcome to the forums :slight_smile:

In your snippet, the file1 output variable is indeed of type StringIO, so you don’t need to use the open function on it. You can directly parse it through xmltodict :

import xmltodict
import json
import streamlit as st

file1 = st.file_uploader('File 1')
file2= st.file_uploader('File 2')

if st.button('Compare Files'):
    xml = file1.read()
    file1_data = json.loads(json.dumps(xmltodict.parse(xml)))
    st.write(file1_data)