如何修复 python 中文件列表中的无属性 'name' 错误
how to fix no atribute 'name' error in a list of files in python
我将 streamlit 与 python 结合使用,以允许用户上传多个文件而不是将内容显示为数据框。
但首先我需要检查它的 csv 类型还是 xls,然后显示类型和名称。
问题是在检查文件类型时崩溃并显示以下错误:
AttributeError: 'list' object has no attribute 'name'
Traceback:
File "F:\AIenv\lib\site-packages\streamlit\script_runner.py", line 333, in _run_script
exec(code, module.__dict__)
File "f:\AIenv\streamlit\app2.py", line 766, in <module>
main()
File "f:\AIenv\streamlit\app2.py", line 300, in main
file_details = {"filename":data.name,
请注意,如果我上传单个文件,脚本 运行 没有错误。
代码:
import streamlit as st
import pandas as pd
def main():
if st.sidebar.checkbox("Multiple Files"):
data = st.sidebar.file_uploader('Multiple Excel files', type=["csv","xlsx","xls"],
accept_multiple_files=True)
for file in data:
file.seek(0)
elif st.sidebar.checkbox("Single File"):
data = st.sidebar.file_uploader("Upload Dataset",type=["csv","xlsx","xls"])
if data is not None:
# display the name and the type of the file
file_details = {"filename":data.name,
"filetype":data.type
}
st.write(file_details)
if __name__=='__main__':
main()
data
在这种情况下应该是 list
类型(您可以检查 type(data)
)。
你能做的就是改变:
if data is not None:
# display the name and the type of the file
file_details = {"filename":data.name,
"filetype":data.type
}
st.write(file_details)
收件人:
if data is not None and len(data) > 0:
st.write("filename {} | filetype: {}".format(data[i].name, data[i].type) for i in range(len(data)))
如果选中“多个文件”,您正在尝试访问文件列表的名称和类型。我建议统一您的 'data' 结构并使其始终是一个列表。然后你必须迭代它:
import streamlit as st
import pandas as pd
def main():
if st.sidebar.checkbox("Multiple Files"):
data = st.sidebar.file_uploader('Multiple Excel files', type=["csv","xlsx","xls"],
accept_multiple_files=True)
for file in data:
file.seek(0)
elif st.sidebar.checkbox("Single File"):
data = st.sidebar.file_uploader("Upload Dataset",type=["csv","xlsx","xls"])
if data is not None:
data = [data]
if data is not None:
for file in data:
# display the name and the type of the file
file_details = {"filename":file.name,
"filetype":file.type
}
st.write(file_details)
if __name__=='__main__':
main()
我将 streamlit 与 python 结合使用,以允许用户上传多个文件而不是将内容显示为数据框。
但首先我需要检查它的 csv 类型还是 xls,然后显示类型和名称。
问题是在检查文件类型时崩溃并显示以下错误:
AttributeError: 'list' object has no attribute 'name'
Traceback:
File "F:\AIenv\lib\site-packages\streamlit\script_runner.py", line 333, in _run_script
exec(code, module.__dict__)
File "f:\AIenv\streamlit\app2.py", line 766, in <module>
main()
File "f:\AIenv\streamlit\app2.py", line 300, in main
file_details = {"filename":data.name,
请注意,如果我上传单个文件,脚本 运行 没有错误。
代码:
import streamlit as st
import pandas as pd
def main():
if st.sidebar.checkbox("Multiple Files"):
data = st.sidebar.file_uploader('Multiple Excel files', type=["csv","xlsx","xls"],
accept_multiple_files=True)
for file in data:
file.seek(0)
elif st.sidebar.checkbox("Single File"):
data = st.sidebar.file_uploader("Upload Dataset",type=["csv","xlsx","xls"])
if data is not None:
# display the name and the type of the file
file_details = {"filename":data.name,
"filetype":data.type
}
st.write(file_details)
if __name__=='__main__':
main()
data
在这种情况下应该是 list
类型(您可以检查 type(data)
)。
你能做的就是改变:
if data is not None:
# display the name and the type of the file
file_details = {"filename":data.name,
"filetype":data.type
}
st.write(file_details)
收件人:
if data is not None and len(data) > 0:
st.write("filename {} | filetype: {}".format(data[i].name, data[i].type) for i in range(len(data)))
如果选中“多个文件”,您正在尝试访问文件列表的名称和类型。我建议统一您的 'data' 结构并使其始终是一个列表。然后你必须迭代它:
import streamlit as st
import pandas as pd
def main():
if st.sidebar.checkbox("Multiple Files"):
data = st.sidebar.file_uploader('Multiple Excel files', type=["csv","xlsx","xls"],
accept_multiple_files=True)
for file in data:
file.seek(0)
elif st.sidebar.checkbox("Single File"):
data = st.sidebar.file_uploader("Upload Dataset",type=["csv","xlsx","xls"])
if data is not None:
data = [data]
if data is not None:
for file in data:
# display the name and the type of the file
file_details = {"filename":file.name,
"filetype":file.type
}
st.write(file_details)
if __name__=='__main__':
main()