如何从st.write中删除前缀

huangapple go评论132阅读模式
英文:

How to remove prefix from st.write

问题

我有一个运行的streamlit应用程序

results = subprocess.check_output(["Rscript", "latest_updates.R"])
st.write(results)

并返回:

b'[[1]]\r\n \r\n1 2023-03-09T12:19:34.3653038Z\r\n\r\n[[2]]\r\n \r\n1 2023-03-09T12:19:33.1931475Z\r\n\r\n[[3]]\r\n \r\n1 2023-03-02T17:12:01.3036067Z\r\n\r\n[[4]]\r\n \r\n1 Mar 9 2023 1:45PM\r\n\r\n[[5]]\r\n \r\n1 2023-03-02T17:08:18.1044511Z\r\n\r\n[[6]]\r\n \r\n1 <NA>\r\n\r\n[[7]]\r\n \r\n1 2023-03-09T10:24:21.1608589Z\r\n\r\n'

这是一个包含7个来自R的时间戳的列表。

我将其转换为列表:

r = results.decode("utf-8").replace("\r\n\r\n","").replace("\r\n \r\n1 ","").replace("[[1]]","").replace("[[2]]","|").replace("[[3]]","|").replace("[[4]]","|").replace("[[5]]","|").replace("[[6]]","|").replace("[[7]]","|").split("|")

并将每个元素写入streamlit:

st.write(r[0])
st.write(r[1])
st.write(r[2])
st.write(r[3])
st.write(r[4])
st.write(r[5])
st.write(r[6])
英文:

I have a streamlit app that runs

results = subprocess.check_output(["Rscript", "latest_updates.R"])
st.write(results)

and returns:

b'[[1]]\r\n \r\n1 2023-03-09T12:19:34.3653038Z\r\n\r\n[[2]]\r\n \r\n1 2023-03-09T12:19:33.1931475Z\r\n\r\n[[3]]\r\n \r\n1 2023-03-02T17:12:01.3036067Z\r\n\r\n[[4]]\r\n \r\n1 Mar 9 2023 1:45PM\r\n\r\n[[5]]\r\n \r\n1 2023-03-02T17:08:18.1044511Z\r\n\r\n[[6]]\r\n \r\n1 <NA>\r\n\r\n[[7]]\r\n \r\n1 2023-03-09T10:24:21.1608589Z\r\n\r\n'

this is a list that contains 7 timestamps from R.

I convert this to a list with:

r = results.decode("utf-8").replace("\r\n\r\n","").replace("\r\n \r\n1 ","").replace("[[1]]","").replace("[[2]]","|").replace("[[3]]","|").replace("[[4]]","|").replace("[[5]]","|").replace("[[6]]","|").replace("[[7]]","|").split("|")

and write each element to streamlit with:

st.write(r[0])
st.write(r[1])
st.write(r[2])
st.write(r[3])
st.write(r[4])
st.write(r[5])
st.write(r[6])

The data are ok, but on the app itself, each string is prefixed with a 1 , and I would like to remove that:

如何从st.write中删除前缀

答案1

得分: 1

import re
import streamlit as st

results = subprocess.check_output(["Rscript", "latest_updates.R"])

res = re.sub(r'\r\n ?\r\n', '', results.decode("utf-8"))  # remove "\r\n \r\n" and "\r\n\r\n"
res = re.sub(r'\[\[[0-9]+\]\]1 ', '\n', res)  # remove the [[1]] and the leading 1

res = [r for r in res.split("\n") if r.strip()]  # remove empty matches

for r in res:
    st.write(r)
英文:

You could clean the output more simply using re. Also, you could use for loops to st.write what you want:

import re
import streamlit as st

results = subprocess.check_output(["Rscript", "latest_updates.R"])

res = re.sub(r'\r\n ?\r\n', '', results.decode("utf-8"))  # remove "\r\n \r\n" and "\r\n\r\n"
res = re.sub(r'\[\[[0-9]+\]\]1 ', '\n', res)  # remove the [[1]] and the leading 1

res = [r for r in res.split("\n") if r.strip()]  # remove empty matches

for r in res:
    st.write(r)

With results looking like this:

b'[[1]]\r\n \r\n1 2023-03-09T12:19:34.3653038Z\r\n\r\n[[2]]\r\n \r\n1 2023-03-09T12:19:33.1931475Z\r\n\r\n[[3]]\r\n \r\n1 2023-03-02T17:12:01.3036067Z\r\n\r\n[[4]]\r\n \r\n1 Mar 9 2023 1:45PM\r\n\r\n[[5]]\r\n \r\n1 2023-03-02T17:08:18.1044511Z\r\n\r\n[[6]]\r\n \r\n1 <NA>\r\n\r\n[[7]]\r\n \r\n1 2023-03-09T10:24:21.1608589Z\r\n\r\n'

it gives:

如何从st.write中删除前缀

huangapple
  • 本文由 发表于 2023年3月10日 01:38:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75688186.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定