0

When I using pandas.to_sql to write a df into mysql, the program gives me the following error:

UnicodeEncodeError: 'latin-1' codec can't encode character u'\u2019' in position 4: ordinal not in range(256)

my code is just as this:

tony.to_sql(con=con_larry, name = 'tony', if_exists='replace', flavor='mysql', chunksize=100)

I don't know why this error presents and how to solve it

shuttle87
  • 15,466
  • 11
  • 77
  • 106

1 Answers1

0

I am not familiar with whatever api you are using here so don't know which variable holds the "problematic string" but do following on the variable that holds the string to be inserted into db

encoded_str = your_data_string.encode('latin-1')
shreyas
  • 2,510
  • 4
  • 19
  • 20
  • actually when I add such code tony['date'] = tony['date'].apply(lambda x: str(x).encode('latin-1')), and tony.to_sql(), still I get the above error, it is the same – Wenrui Luo Jul 10 '15 at 06:23
  • date (assuming it is date object) doesn't look like is a culprit. Is there any other string which is potential from external source like scrapped data or data read from file etc. – shreyas Jul 10 '15 at 12:58