runtime
189 / 500
question 189 Files & Directories · hard

Which supplier files aren't UTF-8?

Suppliers send price lists as CSV files, and some of them still save in an old Windows encoding. Before importing, tell each supplier exactly which line of their file is broken.

Functioncheck_encodings(folder)
Checksevery *.csv file in folder: can its bytes (path.read_bytes()) be decoded as UTF-8?
Returnsa dictionary from file name to None when it decodes, or the line number (from 1) holding the first byte that can't be decoded
Error detailsin except UnicodeDecodeError as err:, err.start is the position of the first bad byte; the line number is how many \n bytes come before it, plus 1
Countingdata[:n].count(b"\n") counts the newline bytes in the first n bytes

Loading your progress…

Which supplier files aren't UTF-8? — Python practice · Runtime