簡體   English   中英

如何在 Streamlit 中使用 split()

[英]How to use split() with Streamlit

我有一個腳本可以打開一個文本文件,讀取列表中的行,然后使用.split()拆分行。

我想在 Streamlit 中使用相同的功能,但split會引發異常:

類型錯誤:需要類似字節的 object,而不是“str”

這是評論中提到的一個最小示例:

import streamlit as st
import pandas as pd

# TIlte of the App
st.title(
"MWE")

#Side bar
st.sidebar.subheader("File Upload")

semi_colon = -5
entry_id = -4

#File upload
uploaded_log = st.sidebar.file_uploader(label="Upload your Log File",type='txt')

if uploaded_log is not None:
    print(uploaded_log)
    try:
    print("Open And readlines")
    eeprom = uploaded_log.readlines()

    print("create lists from the logfiles")
    ##create lists from the logfiles 
    sip_list = [x for x in eeprom if (x[entry_id] == ord('1') and x[semi_colon] == ord(';'))]
    slope_list = [x for x in eeprom if (x[entry_id] == ord('2') and x[semi_colon] == ord(';'))]
    cal1_list = [x for x in eeprom if (x[entry_id] == ord('3') and x[semi_colon] == ord(';'))]
    
    group_of_lists = [sip_list,slope_list,cal1_list]
    group_new = [[l.split(";") for l in group] for group in group_of_lists]

    except:
    print("Error occured")

else:
st.write('Please upload a file to the app')

感謝您的每一條評論,我想我已經找到了答案。 代替:

 group_new = [[l.split(";") for l in group] for group in group_of_lists]

它應該是:

group_new = [[str(l).split(";") for l in group] for group in group_of_lists]

這樣做的原因是這些行仍然是類似字節的對象,首先需要將它們轉換為字符串才能在其上使用字符串函數。 我也找到了關於它的社區帖子: https://discuss.streamlit.io/t/upload-text-file-which-needs-to-be-read-line-by-line/7714/2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM