I am fetching data from remote database and trying to write it with UTF8 encoding as CSV file. This is what I have now:
query = "select * from hotels limit 50000"
output_query = "copy ({0}) to stdout with csv header ecnoding 'UTF8'".format(query)
filename = "hotels.csv"
with open(filename, 'w') as f:
cur.copy_expert(output_query, f)
But this usually throws:
UnicodeEncodeError: 'charmap' codec can't encode character '\u016f' in position 52: character maps to <undefined>
If i change query limit to lets say 1000 mentioned exception does not occur, but if i try to load generated csv file using pandas, following exception occurs:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe4 in position 14: invalid continuation byte
Do you have any idea what is the problem? How to export sql table to csv the proper way? Thanks in advance!