How to remove illegal characters so a dataframe can write to Excel

前端 未结 7 895
广开言路
广开言路 2020-12-03 05:14

I am trying to write a dataframe to an Excel spreadsheet using ExcelWriter, but it keeps returning an error:

openpyxl.utils.exceptions.IllegalCharacterError
         


        
7条回答
  •  盖世英雄少女心
    2020-12-03 05:35

    I was also struggling with some weird characters in a data frame when writing the data frame to html or csv. For example, for characters with accent, I can't write to html file, so I need to convert the characters into characters without the accent.

    My method may not be the best, but it helps me to convert unicode string into ascii compatible.

    # install unidecode first 
    from unidecode import unidecode
    
    def FormatString(s):
    if isinstance(s, unicode):
      try:
        s.encode('ascii')
        return s
      except:
        return unidecode(s)
    else:
      return s
    
    df2 = df1.applymap(FormatString) 
    

    In your situation, if you just want to get rid of the illegal characters by changing return unidecode(s) to return 'StringYouWantToReplace'.

    Hope this can give me some ideas to deal with your problems.

提交回复
热议问题