0

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!

1 Answer 1

2

I should have added encoding='utf-8' in open function.

Sign up to request clarification or add additional context in comments.

1 Comment

It's good practice to always set the encoding in your open() commands. There's a Semgrep rule for this if you use CI, that can catch it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.