最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - FileUpload ipywidget: showing Error tuple - Stack Overflow

programmeradmin5浏览0评论

i got a problem with the Fileupload Widget in Jupyter

import pandas as pd
import ipywidgets as widgets
from ipywidgets import FileUpload
from IPython.display import display

upload = FileUpload(accept='.xlsx', multiple=False)

def on_file_upload(change):
    uploaded_file = list(change['new'].values())[0]
    content = uploaded_file['content']    
    df = pd.read_excel(content)    
    display(df.head())

upload.observe(on_file_upload, names='value')
display(upload)

In uploaded_file = list(change['new'].values())[0] i get the error AttributeError: 'tuple' object has no attribute 'values'

Already tried everything and read the documentation but cant find a solution. Hope someone can help.

A table is in the excel and i got 2 sheets inside.

i got a problem with the Fileupload Widget in Jupyter

import pandas as pd
import ipywidgets as widgets
from ipywidgets import FileUpload
from IPython.display import display

upload = FileUpload(accept='.xlsx', multiple=False)

def on_file_upload(change):
    uploaded_file = list(change['new'].values())[0]
    content = uploaded_file['content']    
    df = pd.read_excel(content)    
    display(df.head())

upload.observe(on_file_upload, names='value')
display(upload)

In uploaded_file = list(change['new'].values())[0] i get the error AttributeError: 'tuple' object has no attribute 'values'

Already tried everything and read the documentation but cant find a solution. Hope someone can help.

A table is in the excel and i got 2 sheets inside.

Share Improve this question asked Feb 14 at 7:34 credencocredenco 2872 silver badges14 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

Are you sure that uploaded file get dictionary from which you are getting 'new' key and try to get the values() from it.

First of all check the type of "change" that is it dictionary or tuple, if yes it is dictionary then do the given below code otherwise it will show error every time.

uploaded_file = list(change['new'].values())[0]

With the help of this post i was able to make this.

import ipywidgets as widgets
import pandas as pd
from IPython.display import display
import io

file_upload = widgets.FileUpload()
output = widgets.Output()

df_uploaded = None

def on_upload_change(change):
    global df_uploaded
    with output:
        output.clear_output()
        file_contents = change.new[0].content
        df_uploaded = read_excelfile(file_contents)  
        display(df_uploaded)

def read_excelfile(file_content):
     df = pd.read_excel(io.BytesIO(file_content))
     return df

file_upload.observe(on_upload_change, names='value')
display(file_upload)
display(output)

ChatGPT helped with the df_uploaded = None. I dont understand why this is needed.

发布评论

评论列表(0)

  1. 暂无评论