Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
482 views
in Technique[技术] by (71.8m points)

Python: TypeError: Value must be a sequence

My code:

def write_excel(filename, sheetname, dataframe):
    with pd.ExcelWriter(filename, engine='openpyxl', mode='a') as writer:
        workBook = writer.book
        try:
            workBook.remove(workBook[sheetname])
        except:
            print("Worksheet does not exist")
        finally:
            dataframe.to_excel(writer, sheet_name=sheetname, index=False)
            writer.save()


write_excel(r"C:UsersHarshadDocumentsProjectFilesBacktest.xlsx", '888', df_normalized)

Error at:

 write_excel(r"C:UsersHarshadDocumentsProjectFilesBacktest.xlsx", '888', df_normalized)

I am getting at error as:

Traceback (most recent call last):
  File "C:/Users/Harshad/Documents/Project/Code/Step-4-Normalizing 888Sport Odds.py", line 66, in <module>
    write_excel(r"C:UsersHarshadDocumentsProjectFilesBacktest.xlsx", '888', df_normalized)
  File "C:/Users/Harshad/Documents/Project/Code/Step-4-Normalizing 888Sport Odds.py", line 55, in write_excel
    with pd.ExcelWriter(filename, engine='openpyxl', mode='a') as writer:
  File "C:UsersHarshadPycharmProjectsSelf Projectvenvlibsite-packagespandasioexcel\_openpyxl.py", line 25, in __init__
    book = load_workbook(self.path)
  File "C:UsersHarshadPycharmProjectsSelf Projectvenvlibsite-packagesopenpyxl
eaderexcel.py", line 315, in load_workbook
    reader.read()
  File "C:UsersHarshadPycharmProjectsSelf Projectvenvlibsite-packagesopenpyxl
eaderexcel.py", line 280, in read
    self.read_worksheets()
  File "C:UsersHarshadPycharmProjectsSelf Projectvenvlibsite-packagesopenpyxl
eaderexcel.py", line 266, in read_worksheets
    pivot = TableDefinition.from_tree(tree)
  File "C:UsersHarshadPycharmProjectsSelf Projectvenvlibsite-packagesopenpyxldescriptorsserialisable.py", line 83, in from_tree
    obj = desc.from_tree(el)
  File "C:UsersHarshadPycharmProjectsSelf Projectvenvlibsite-packagesopenpyxldescriptorssequence.py", line 85, in from_tree
    return [self.expected_type.from_tree(el) for el in node]
  File "C:UsersHarshadPycharmProjectsSelf Projectvenvlibsite-packagesopenpyxldescriptorssequence.py", line 85, in <listcomp>
    return [self.expected_type.from_tree(el) for el in node]
  File "C:UsersHarshadPycharmProjectsSelf Projectvenvlibsite-packagesopenpyxldescriptorsserialisable.py", line 103, in from_tree
    return cls(**attrib)
  File "C:UsersHarshadPycharmProjectsSelf Projectvenvlibsite-packagesopenpyxlpivotable.py", line 601, in __init__
    self.x = x
  File "C:UsersHarshadPycharmProjectsSelf Projectvenvlibsite-packagesopenpyxldescriptorssequence.py", line 25, in __set__
    raise TypeError("Value must be a sequence")
TypeError: Value must be a sequence

This code worked till yesterday. How can I resolve it? Thanks

question from:https://stackoverflow.com/questions/66057374/python-typeerror-value-must-be-a-sequence

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Don't know if it will help, but the engine parameter is deprecated. This other answer looks promising though:

Solution: Pandas Excel Writer using Openpyxl with existing workbook

Docs: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.ExcelWriter.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...