[{"data":1,"prerenderedAt":1642},["ShallowReactive",2],{"doc-\u002Ferrors\u002Funicodedecodeerror-utf-8-codec-cant-decode-byte-fix":3},{"id":4,"title":5,"body":6,"description":1635,"extension":1636,"meta":1637,"navigation":190,"path":1638,"seo":1639,"stem":1640,"__hash__":1641},"content\u002Ferrors\u002Funicodedecodeerror-utf-8-codec-cant-decode-byte-fix.md","UnicodeDecodeError: 'utf-8' codec can't decode byte (Fix)",{"type":7,"value":8,"toc":1600},"minimark",[9,19,23,65,68,71,87,95,100,206,209,213,216,257,260,273,276,280,283,306,319,323,326,401,407,413,437,440,454,458,463,468,543,549,623,633,640,643,718,721,733,736,739,828,831,919,922,937,940,944,947,950,1013,1016,1087,1090,1113,1117,1120,1123,1143,1146,1161,1168,1172,1175,1179,1182,1240,1243,1254,1260,1322,1328,1389,1395,1456,1463,1467,1470,1487,1491,1494,1516,1520,1524,1527,1534,1537,1546,1555,1559,1565,1569,1596],[10,11,13,14,18],"h1",{"id":12},"unicodedecodeerror-utf-8-codec-cant-decode-byte-fix","UnicodeDecodeError: ",[15,16,17],"code",{},"'utf-8' codec can't decode byte"," (Fix)",[20,21,22],"p",{},"If you see a Python error like this:",[24,25,30],"pre",{"className":26,"code":27,"language":28,"meta":29,"style":29},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","UnicodeDecodeError: 'utf-8' codec can't decode byte ...\n","python","",[15,31,32],{"__ignoreMap":29},[33,34,37,41,45,49,53,56,60,62],"span",{"class":35,"line":36},"line",1,[33,38,40],{"class":39},"sZMiF","UnicodeDecodeError",[33,42,44],{"class":43},"sP7_E",":",[33,46,48],{"class":47},"sjJ54"," '",[33,50,52],{"class":51},"s_sjI","utf-8",[33,54,55],{"class":47},"'",[33,57,59],{"class":58},"su5hD"," codec can",[33,61,55],{"class":47},[33,63,64],{"class":51},"t decode byte ...\n",[20,66,67],{},"Python is trying to read data as UTF-8 text, but the bytes do not match valid UTF-8.",[20,69,70],{},"This usually happens when:",[72,73,74,78,81,84],"ul",{},[75,76,77],"li",{},"you open a text file with the wrong encoding",[75,79,80],{},"a CSV file came from Excel or another Windows program",[75,82,83],{},"you decode bytes from a file, download, or API using the wrong encoding",[75,85,86],{},"the file is actually binary, not text",[20,88,89,90,94],{},"In most cases, the fix is to open the file with the ",[91,92,93],"strong",{},"correct encoding"," instead of relying on the default.",[96,97,99],"h2",{"id":98},"quick-fix","Quick fix",[24,101,103],{"className":26,"code":102,"language":28,"meta":29,"style":29},"with open('data.txt', 'r', encoding='latin-1') as file:\n    text = file.read()\n\nprint(text)\n",[15,104,105,165,185,192],{"__ignoreMap":29},[33,106,107,111,115,118,120,123,125,128,130,133,135,137,141,145,147,150,152,155,158,162],{"class":35,"line":36},[33,108,110],{"class":109},"sVHd0","with",[33,112,114],{"class":113},"sptTA"," open",[33,116,117],{"class":43},"(",[33,119,55],{"class":47},[33,121,122],{"class":51},"data.txt",[33,124,55],{"class":47},[33,126,127],{"class":43},",",[33,129,48],{"class":47},[33,131,132],{"class":51},"r",[33,134,55],{"class":47},[33,136,127],{"class":43},[33,138,140],{"class":139},"s99_P"," encoding",[33,142,144],{"class":143},"smGrS","=",[33,146,55],{"class":47},[33,148,149],{"class":51},"latin-1",[33,151,55],{"class":47},[33,153,154],{"class":43},")",[33,156,157],{"class":109}," as",[33,159,161],{"class":160},"sMMDD"," file",[33,163,164],{"class":43},":\n",[33,166,168,171,173,175,178,182],{"class":35,"line":167},2,[33,169,170],{"class":58},"    text ",[33,172,144],{"class":143},[33,174,161],{"class":160},[33,176,177],{"class":43},".",[33,179,181],{"class":180},"slqww","read",[33,183,184],{"class":43},"()\n",[33,186,188],{"class":35,"line":187},3,[33,189,191],{"emptyLinePlaceholder":190},true,"\n",[33,193,195,198,200,203],{"class":35,"line":194},4,[33,196,197],{"class":113},"print",[33,199,117],{"class":43},[33,201,202],{"class":180},"text",[33,204,205],{"class":43},")\n",[20,207,208],{},"Use the correct file encoding instead of the default UTF-8. If you do not know the encoding, first check where the file came from.",[96,210,212],{"id":211},"what-this-error-means","What this error means",[20,214,215],{},"This error means:",[72,217,218,228,231,234],{},[75,219,220,221,224,225,227],{},"Python is trying to turn ",[91,222,223],{},"bytes"," into ",[91,226,202],{}," using UTF-8",[75,229,230],{},"at least one byte is not valid in UTF-8",[75,232,233],{},"the file or data was probably saved with a different encoding",[75,235,236,237],{},"common encodings include:\n",[72,238,239,243,247,252],{},[75,240,241],{},[15,242,52],{},[75,244,245],{},[15,246,149],{},[75,248,249],{},[15,250,251],{},"cp1252",[75,253,254],{},[15,255,256],{},"utf-16",[20,258,259],{},"A simple way to think about it:",[72,261,262,267],{},[75,263,264,266],{},[91,265,223],{}," are raw data",[75,268,269,272],{},[91,270,271],{},"encoding"," tells Python how to interpret those bytes as text",[20,274,275],{},"If Python uses the wrong encoding, it cannot read the text correctly.",[96,277,279],{"id":278},"when-this-error-usually-happens","When this error usually happens",[20,281,282],{},"You will often see this error when:",[72,284,285,294,297,300],{},[75,286,287,288,291,292],{},"reading a text file with ",[15,289,290],{},"open()"," without the right ",[15,293,271],{},[75,295,296],{},"loading CSV or text data created on another system",[75,298,299],{},"decoding raw bytes from a network response or binary source",[75,301,302,303,305],{},"reading Windows-created files that use ",[15,304,251],{}," instead of UTF-8",[20,307,308,309,314,315,177],{},"If you are new to file reading, see ",[310,311,313],"a",{"href":312},"\u002Fhow-to\u002Fhow-to-read-a-file-in-python\u002F","how to read a file in Python"," and ",[310,316,318],{"href":317},"\u002Flearn\u002Fpython-file-handling-basics-read-and-write\u002F","Python file handling basics",[96,320,322],{"id":321},"example-that-causes-the-error","Example that causes the error",[20,324,325],{},"Here is a typical example.",[24,327,329],{"className":26,"code":328,"language":28,"meta":29,"style":29},"with open('data.txt', 'r', encoding='utf-8') as file:\n    text = file.read()\n\nprint(text)\n",[15,330,331,373,387,391],{"__ignoreMap":29},[33,332,333,335,337,339,341,343,345,347,349,351,353,355,357,359,361,363,365,367,369,371],{"class":35,"line":36},[33,334,110],{"class":109},[33,336,114],{"class":113},[33,338,117],{"class":43},[33,340,55],{"class":47},[33,342,122],{"class":51},[33,344,55],{"class":47},[33,346,127],{"class":43},[33,348,48],{"class":47},[33,350,132],{"class":51},[33,352,55],{"class":47},[33,354,127],{"class":43},[33,356,140],{"class":139},[33,358,144],{"class":143},[33,360,55],{"class":47},[33,362,52],{"class":51},[33,364,55],{"class":47},[33,366,154],{"class":43},[33,368,157],{"class":109},[33,370,161],{"class":160},[33,372,164],{"class":43},[33,374,375,377,379,381,383,385],{"class":35,"line":167},[33,376,170],{"class":58},[33,378,144],{"class":143},[33,380,161],{"class":160},[33,382,177],{"class":43},[33,384,181],{"class":180},[33,386,184],{"class":43},[33,388,389],{"class":35,"line":187},[33,390,191],{"emptyLinePlaceholder":190},[33,392,393,395,397,399],{"class":35,"line":194},[33,394,197],{"class":113},[33,396,117],{"class":43},[33,398,202],{"class":180},[33,400,205],{"class":43},[20,402,403,404,406],{},"This code works only if ",[15,405,122],{}," is really saved as UTF-8.",[20,408,409,410,412],{},"If the file actually uses another encoding such as ",[15,411,251],{},", Python may raise:",[24,414,416],{"className":26,"code":415,"language":28,"meta":29,"style":29},"UnicodeDecodeError: 'utf-8' codec can't decode byte 0x92 in position 10: invalid start byte\n",[15,417,418],{"__ignoreMap":29},[33,419,420,422,424,426,428,430,432,434],{"class":35,"line":36},[33,421,40],{"class":39},[33,423,44],{"class":43},[33,425,48],{"class":47},[33,427,52],{"class":51},[33,429,55],{"class":47},[33,431,59],{"class":58},[33,433,55],{"class":47},[33,435,436],{"class":51},"t decode byte 0x92 in position 10: invalid start byte\n",[20,438,439],{},"What happened:",[72,441,442,445,448,451],{},[75,443,444],{},"the file was opened in text mode",[75,446,447],{},"Python tried to decode it as UTF-8",[75,449,450],{},"one or more bytes were invalid for UTF-8",[75,452,453],{},"reading failed",[96,455,457],{"id":456},"main-ways-to-fix-it","Main ways to fix it",[459,460,462],"h3",{"id":461},"open-the-file-with-the-correct-encoding","Open the file with the correct encoding",[20,464,465,466,177],{},"If you know the file encoding, pass it to ",[15,467,290],{},[24,469,471],{"className":26,"code":470,"language":28,"meta":29,"style":29},"with open('data.txt', 'r', encoding='cp1252') as file:\n    text = file.read()\n\nprint(text)\n",[15,472,473,515,529,533],{"__ignoreMap":29},[33,474,475,477,479,481,483,485,487,489,491,493,495,497,499,501,503,505,507,509,511,513],{"class":35,"line":36},[33,476,110],{"class":109},[33,478,114],{"class":113},[33,480,117],{"class":43},[33,482,55],{"class":47},[33,484,122],{"class":51},[33,486,55],{"class":47},[33,488,127],{"class":43},[33,490,48],{"class":47},[33,492,132],{"class":51},[33,494,55],{"class":47},[33,496,127],{"class":43},[33,498,140],{"class":139},[33,500,144],{"class":143},[33,502,55],{"class":47},[33,504,251],{"class":51},[33,506,55],{"class":47},[33,508,154],{"class":43},[33,510,157],{"class":109},[33,512,161],{"class":160},[33,514,164],{"class":43},[33,516,517,519,521,523,525,527],{"class":35,"line":167},[33,518,170],{"class":58},[33,520,144],{"class":143},[33,522,161],{"class":160},[33,524,177],{"class":43},[33,526,181],{"class":180},[33,528,184],{"class":43},[33,530,531],{"class":35,"line":187},[33,532,191],{"emptyLinePlaceholder":190},[33,534,535,537,539,541],{"class":35,"line":194},[33,536,197],{"class":113},[33,538,117],{"class":43},[33,540,202],{"class":180},[33,542,205],{"class":43},[20,544,545,546,548],{},"You can also try ",[15,547,149],{}," if that matches the source:",[24,550,551],{"className":26,"code":102,"language":28,"meta":29,"style":29},[15,552,553,595,609,613],{"__ignoreMap":29},[33,554,555,557,559,561,563,565,567,569,571,573,575,577,579,581,583,585,587,589,591,593],{"class":35,"line":36},[33,556,110],{"class":109},[33,558,114],{"class":113},[33,560,117],{"class":43},[33,562,55],{"class":47},[33,564,122],{"class":51},[33,566,55],{"class":47},[33,568,127],{"class":43},[33,570,48],{"class":47},[33,572,132],{"class":51},[33,574,55],{"class":47},[33,576,127],{"class":43},[33,578,140],{"class":139},[33,580,144],{"class":143},[33,582,55],{"class":47},[33,584,149],{"class":51},[33,586,55],{"class":47},[33,588,154],{"class":43},[33,590,157],{"class":109},[33,592,161],{"class":160},[33,594,164],{"class":43},[33,596,597,599,601,603,605,607],{"class":35,"line":167},[33,598,170],{"class":58},[33,600,144],{"class":143},[33,602,161],{"class":160},[33,604,177],{"class":43},[33,606,181],{"class":180},[33,608,184],{"class":43},[33,610,611],{"class":35,"line":187},[33,612,191],{"emptyLinePlaceholder":190},[33,614,615,617,619,621],{"class":35,"line":194},[33,616,197],{"class":113},[33,618,117],{"class":43},[33,620,202],{"class":180},[33,622,205],{"class":43},[20,624,625,626,628,629,177],{},"For a beginner-friendly explanation of ",[15,627,290],{},", see ",[310,630,632],{"href":631},"\u002Freference\u002Fpython-open-function-explained\u002F","Python open() function explained",[459,634,636,637],{"id":635},"if-the-file-has-a-utf-8-bom-try-utf-8-sig","If the file has a UTF-8 BOM, try ",[15,638,639],{},"utf-8-sig",[20,641,642],{},"Some files are UTF-8 but include a special marker at the start called a BOM.",[24,644,646],{"className":26,"code":645,"language":28,"meta":29,"style":29},"with open('data.txt', 'r', encoding='utf-8-sig') as file:\n    text = file.read()\n\nprint(text)\n",[15,647,648,690,704,708],{"__ignoreMap":29},[33,649,650,652,654,656,658,660,662,664,666,668,670,672,674,676,678,680,682,684,686,688],{"class":35,"line":36},[33,651,110],{"class":109},[33,653,114],{"class":113},[33,655,117],{"class":43},[33,657,55],{"class":47},[33,659,122],{"class":51},[33,661,55],{"class":47},[33,663,127],{"class":43},[33,665,48],{"class":47},[33,667,132],{"class":51},[33,669,55],{"class":47},[33,671,127],{"class":43},[33,673,140],{"class":139},[33,675,144],{"class":143},[33,677,55],{"class":47},[33,679,639],{"class":51},[33,681,55],{"class":47},[33,683,154],{"class":43},[33,685,157],{"class":109},[33,687,161],{"class":160},[33,689,164],{"class":43},[33,691,692,694,696,698,700,702],{"class":35,"line":167},[33,693,170],{"class":58},[33,695,144],{"class":143},[33,697,161],{"class":160},[33,699,177],{"class":43},[33,701,181],{"class":180},[33,703,184],{"class":43},[33,705,706],{"class":35,"line":187},[33,707,191],{"emptyLinePlaceholder":190},[33,709,710,712,714,716],{"class":35,"line":194},[33,711,197],{"class":113},[33,713,117],{"class":43},[33,715,202],{"class":180},[33,717,205],{"class":43},[20,719,720],{},"This is often useful for text files saved by certain editors or export tools.",[459,722,724,725,728,729,732],{"id":723},"use-errorsignore-or-errorsreplace-only-if-needed","Use ",[15,726,727],{},"errors='ignore'"," or ",[15,730,731],{},"errors='replace'"," only if needed",[20,734,735],{},"If you must keep reading even when some characters are bad, you can tell Python how to handle decode errors.",[20,737,738],{},"Ignore bad characters:",[24,740,742],{"className":26,"code":741,"language":28,"meta":29,"style":29},"with open('data.txt', 'r', encoding='utf-8', errors='ignore') as file:\n    text = file.read()\n\nprint(text)\n",[15,743,744,800,814,818],{"__ignoreMap":29},[33,745,746,748,750,752,754,756,758,760,762,764,766,768,770,772,774,776,778,780,783,785,787,790,792,794,796,798],{"class":35,"line":36},[33,747,110],{"class":109},[33,749,114],{"class":113},[33,751,117],{"class":43},[33,753,55],{"class":47},[33,755,122],{"class":51},[33,757,55],{"class":47},[33,759,127],{"class":43},[33,761,48],{"class":47},[33,763,132],{"class":51},[33,765,55],{"class":47},[33,767,127],{"class":43},[33,769,140],{"class":139},[33,771,144],{"class":143},[33,773,55],{"class":47},[33,775,52],{"class":51},[33,777,55],{"class":47},[33,779,127],{"class":43},[33,781,782],{"class":139}," errors",[33,784,144],{"class":143},[33,786,55],{"class":47},[33,788,789],{"class":51},"ignore",[33,791,55],{"class":47},[33,793,154],{"class":43},[33,795,157],{"class":109},[33,797,161],{"class":160},[33,799,164],{"class":43},[33,801,802,804,806,808,810,812],{"class":35,"line":167},[33,803,170],{"class":58},[33,805,144],{"class":143},[33,807,161],{"class":160},[33,809,177],{"class":43},[33,811,181],{"class":180},[33,813,184],{"class":43},[33,815,816],{"class":35,"line":187},[33,817,191],{"emptyLinePlaceholder":190},[33,819,820,822,824,826],{"class":35,"line":194},[33,821,197],{"class":113},[33,823,117],{"class":43},[33,825,202],{"class":180},[33,827,205],{"class":43},[20,829,830],{},"Replace bad characters:",[24,832,834],{"className":26,"code":833,"language":28,"meta":29,"style":29},"with open('data.txt', 'r', encoding='utf-8', errors='replace') as file:\n    text = file.read()\n\nprint(text)\n",[15,835,836,891,905,909],{"__ignoreMap":29},[33,837,838,840,842,844,846,848,850,852,854,856,858,860,862,864,866,868,870,872,874,876,878,881,883,885,887,889],{"class":35,"line":36},[33,839,110],{"class":109},[33,841,114],{"class":113},[33,843,117],{"class":43},[33,845,55],{"class":47},[33,847,122],{"class":51},[33,849,55],{"class":47},[33,851,127],{"class":43},[33,853,48],{"class":47},[33,855,132],{"class":51},[33,857,55],{"class":47},[33,859,127],{"class":43},[33,861,140],{"class":139},[33,863,144],{"class":143},[33,865,55],{"class":47},[33,867,52],{"class":51},[33,869,55],{"class":47},[33,871,127],{"class":43},[33,873,782],{"class":139},[33,875,144],{"class":143},[33,877,55],{"class":47},[33,879,880],{"class":51},"replace",[33,882,55],{"class":47},[33,884,154],{"class":43},[33,886,157],{"class":109},[33,888,161],{"class":160},[33,890,164],{"class":43},[33,892,893,895,897,899,901,903],{"class":35,"line":167},[33,894,170],{"class":58},[33,896,144],{"class":143},[33,898,161],{"class":160},[33,900,177],{"class":43},[33,902,181],{"class":180},[33,904,184],{"class":43},[33,906,907],{"class":35,"line":187},[33,908,191],{"emptyLinePlaceholder":190},[33,910,911,913,915,917],{"class":35,"line":194},[33,912,197],{"class":113},[33,914,117],{"class":43},[33,916,202],{"class":180},[33,918,205],{"class":43},[20,920,921],{},"Use this carefully:",[72,923,924,929,934],{},[75,925,926,928],{},[15,927,789],{}," silently removes characters",[75,930,931,933],{},[15,932,880],{}," changes them to replacement symbols",[75,935,936],{},"both can hide the real problem",[20,938,939],{},"If the text matters, finding the correct encoding is safer.",[459,941,943],{"id":942},"open-binary-files-in-binary-mode","Open binary files in binary mode",[20,945,946],{},"If the file is not really text, do not open it in text mode.",[20,948,949],{},"Wrong:",[24,951,953],{"className":26,"code":952,"language":28,"meta":29,"style":29},"with open('image.jpg', 'r', encoding='utf-8') as file:\n    data = file.read()\n",[15,954,955,998],{"__ignoreMap":29},[33,956,957,959,961,963,965,968,970,972,974,976,978,980,982,984,986,988,990,992,994,996],{"class":35,"line":36},[33,958,110],{"class":109},[33,960,114],{"class":113},[33,962,117],{"class":43},[33,964,55],{"class":47},[33,966,967],{"class":51},"image.jpg",[33,969,55],{"class":47},[33,971,127],{"class":43},[33,973,48],{"class":47},[33,975,132],{"class":51},[33,977,55],{"class":47},[33,979,127],{"class":43},[33,981,140],{"class":139},[33,983,144],{"class":143},[33,985,55],{"class":47},[33,987,52],{"class":51},[33,989,55],{"class":47},[33,991,154],{"class":43},[33,993,157],{"class":109},[33,995,161],{"class":160},[33,997,164],{"class":43},[33,999,1000,1003,1005,1007,1009,1011],{"class":35,"line":167},[33,1001,1002],{"class":58},"    data ",[33,1004,144],{"class":143},[33,1006,161],{"class":160},[33,1008,177],{"class":43},[33,1010,181],{"class":180},[33,1012,184],{"class":43},[20,1014,1015],{},"Correct:",[24,1017,1019],{"className":26,"code":1018,"language":28,"meta":29,"style":29},"with open('image.jpg', 'rb') as file:\n    data = file.read()\n\nprint(type(data))\n",[15,1020,1021,1052,1066,1070],{"__ignoreMap":29},[33,1022,1023,1025,1027,1029,1031,1033,1035,1037,1039,1042,1044,1046,1048,1050],{"class":35,"line":36},[33,1024,110],{"class":109},[33,1026,114],{"class":113},[33,1028,117],{"class":43},[33,1030,55],{"class":47},[33,1032,967],{"class":51},[33,1034,55],{"class":47},[33,1036,127],{"class":43},[33,1038,48],{"class":47},[33,1040,1041],{"class":51},"rb",[33,1043,55],{"class":47},[33,1045,154],{"class":43},[33,1047,157],{"class":109},[33,1049,161],{"class":160},[33,1051,164],{"class":43},[33,1053,1054,1056,1058,1060,1062,1064],{"class":35,"line":167},[33,1055,1002],{"class":58},[33,1057,144],{"class":143},[33,1059,161],{"class":160},[33,1061,177],{"class":43},[33,1063,181],{"class":180},[33,1065,184],{"class":43},[33,1067,1068],{"class":35,"line":187},[33,1069,191],{"emptyLinePlaceholder":190},[33,1071,1072,1074,1076,1079,1081,1084],{"class":35,"line":194},[33,1073,197],{"class":113},[33,1075,117],{"class":43},[33,1077,1078],{"class":39},"type",[33,1080,117],{"class":43},[33,1082,1083],{"class":180},"data",[33,1085,1086],{"class":43},"))\n",[20,1088,1089],{},"Output:",[24,1091,1093],{"className":26,"code":1092,"language":28,"meta":29,"style":29},"\u003Cclass 'bytes'>\n",[15,1094,1095],{"__ignoreMap":29},[33,1096,1097,1100,1104,1106,1108,1110],{"class":35,"line":36},[33,1098,1099],{"class":143},"\u003C",[33,1101,1103],{"class":1102},"sbsja","class",[33,1105,48],{"class":47},[33,1107,223],{"class":51},[33,1109,55],{"class":47},[33,1111,1112],{"class":143},">\n",[96,1114,1116],{"id":1115},"how-to-find-the-right-encoding","How to find the right encoding",[20,1118,1119],{},"The best way is to check where the file came from.",[20,1121,1122],{},"Useful clues:",[72,1124,1125,1128,1133,1138],{},[75,1126,1127],{},"files from modern tools and APIs are often UTF-8",[75,1129,1130,1131],{},"text files from Windows programs often use ",[15,1132,251],{},[75,1134,1135,1136],{},"some exported files use ",[15,1137,639],{},[75,1139,1140,1141],{},"older data may use ",[15,1142,149],{},[20,1144,1145],{},"Try these steps:",[1147,1148,1149,1152,1155,1158],"ol",{},[75,1150,1151],{},"Check how the file was created or exported.",[75,1153,1154],{},"Look at the program that produced it.",[75,1156,1157],{},"Test common encodings used by that source.",[75,1159,1160],{},"If it is a CSV file, check the export settings.",[20,1162,1163,1164,177],{},"CSV files are a common cause of this issue. If that is your case, see ",[310,1165,1167],{"href":1166},"\u002Fhow-to\u002Fhow-to-read-a-csv-file-in-python\u002F","how to read a CSV file in Python",[96,1169,1171],{"id":1170},"debugging-steps","Debugging steps",[20,1173,1174],{},"These quick tests can help you find the problem.",[459,1176,1178],{"id":1177},"_1-print-the-raw-bytes","1. Print the raw bytes",[20,1180,1181],{},"Open the file in binary mode and inspect the start of the file:",[24,1183,1185],{"className":26,"code":1184,"language":28,"meta":29,"style":29},"with open('data.txt', 'rb') as f:\n    print(f.read(40))\n",[15,1186,1187,1218],{"__ignoreMap":29},[33,1188,1189,1191,1193,1195,1197,1199,1201,1203,1205,1207,1209,1211,1213,1216],{"class":35,"line":36},[33,1190,110],{"class":109},[33,1192,114],{"class":113},[33,1194,117],{"class":43},[33,1196,55],{"class":47},[33,1198,122],{"class":51},[33,1200,55],{"class":47},[33,1202,127],{"class":43},[33,1204,48],{"class":47},[33,1206,1041],{"class":51},[33,1208,55],{"class":47},[33,1210,154],{"class":43},[33,1212,157],{"class":109},[33,1214,1215],{"class":58}," f",[33,1217,164],{"class":43},[33,1219,1220,1223,1225,1228,1230,1232,1234,1238],{"class":35,"line":167},[33,1221,1222],{"class":113},"    print",[33,1224,117],{"class":43},[33,1226,1227],{"class":180},"f",[33,1229,177],{"class":43},[33,1231,181],{"class":180},[33,1233,117],{"class":43},[33,1235,1237],{"class":1236},"srdBf","40",[33,1239,1086],{"class":43},[20,1241,1242],{},"This helps you confirm that:",[72,1244,1245,1248,1251],{},[75,1246,1247],{},"the file exists",[75,1249,1250],{},"the file contains bytes",[75,1252,1253],{},"the content may not be plain UTF-8 text",[459,1255,1257,1258],{"id":1256},"_2-try-utf-8-sig","2. Try ",[15,1259,639],{},[24,1261,1263],{"className":26,"code":1262,"language":28,"meta":29,"style":29},"with open('data.txt', 'r', encoding='utf-8-sig') as f:\n    print(f.read())\n",[15,1264,1265,1307],{"__ignoreMap":29},[33,1266,1267,1269,1271,1273,1275,1277,1279,1281,1283,1285,1287,1289,1291,1293,1295,1297,1299,1301,1303,1305],{"class":35,"line":36},[33,1268,110],{"class":109},[33,1270,114],{"class":113},[33,1272,117],{"class":43},[33,1274,55],{"class":47},[33,1276,122],{"class":51},[33,1278,55],{"class":47},[33,1280,127],{"class":43},[33,1282,48],{"class":47},[33,1284,132],{"class":51},[33,1286,55],{"class":47},[33,1288,127],{"class":43},[33,1290,140],{"class":139},[33,1292,144],{"class":143},[33,1294,55],{"class":47},[33,1296,639],{"class":51},[33,1298,55],{"class":47},[33,1300,154],{"class":43},[33,1302,157],{"class":109},[33,1304,1215],{"class":58},[33,1306,164],{"class":43},[33,1308,1309,1311,1313,1315,1317,1319],{"class":35,"line":167},[33,1310,1222],{"class":113},[33,1312,117],{"class":43},[33,1314,1227],{"class":180},[33,1316,177],{"class":43},[33,1318,181],{"class":180},[33,1320,1321],{"class":43},"())\n",[459,1323,1325,1326],{"id":1324},"_3-try-cp1252","3. Try ",[15,1327,251],{},[24,1329,1331],{"className":26,"code":1330,"language":28,"meta":29,"style":29},"with open('data.txt', 'r', encoding='cp1252') as f:\n    print(f.read())\n",[15,1332,1333,1375],{"__ignoreMap":29},[33,1334,1335,1337,1339,1341,1343,1345,1347,1349,1351,1353,1355,1357,1359,1361,1363,1365,1367,1369,1371,1373],{"class":35,"line":36},[33,1336,110],{"class":109},[33,1338,114],{"class":113},[33,1340,117],{"class":43},[33,1342,55],{"class":47},[33,1344,122],{"class":51},[33,1346,55],{"class":47},[33,1348,127],{"class":43},[33,1350,48],{"class":47},[33,1352,132],{"class":51},[33,1354,55],{"class":47},[33,1356,127],{"class":43},[33,1358,140],{"class":139},[33,1360,144],{"class":143},[33,1362,55],{"class":47},[33,1364,251],{"class":51},[33,1366,55],{"class":47},[33,1368,154],{"class":43},[33,1370,157],{"class":109},[33,1372,1215],{"class":58},[33,1374,164],{"class":43},[33,1376,1377,1379,1381,1383,1385,1387],{"class":35,"line":167},[33,1378,1222],{"class":113},[33,1380,117],{"class":43},[33,1382,1227],{"class":180},[33,1384,177],{"class":43},[33,1386,181],{"class":180},[33,1388,1321],{"class":43},[459,1390,1392,1393],{"id":1391},"_4-try-latin-1","4. Try ",[15,1394,149],{},[24,1396,1398],{"className":26,"code":1397,"language":28,"meta":29,"style":29},"with open('data.txt', 'r', encoding='latin-1') as f:\n    print(f.read())\n",[15,1399,1400,1442],{"__ignoreMap":29},[33,1401,1402,1404,1406,1408,1410,1412,1414,1416,1418,1420,1422,1424,1426,1428,1430,1432,1434,1436,1438,1440],{"class":35,"line":36},[33,1403,110],{"class":109},[33,1405,114],{"class":113},[33,1407,117],{"class":43},[33,1409,55],{"class":47},[33,1411,122],{"class":51},[33,1413,55],{"class":47},[33,1415,127],{"class":43},[33,1417,48],{"class":47},[33,1419,132],{"class":51},[33,1421,55],{"class":47},[33,1423,127],{"class":43},[33,1425,140],{"class":139},[33,1427,144],{"class":143},[33,1429,55],{"class":47},[33,1431,149],{"class":51},[33,1433,55],{"class":47},[33,1435,154],{"class":43},[33,1437,157],{"class":109},[33,1439,1215],{"class":58},[33,1441,164],{"class":43},[33,1443,1444,1446,1448,1450,1452,1454],{"class":35,"line":167},[33,1445,1222],{"class":113},[33,1447,117],{"class":43},[33,1449,1227],{"class":180},[33,1451,177],{"class":43},[33,1453,181],{"class":180},[33,1455,1321],{"class":43},[20,1457,1458,1459,177],{},"Also make sure you are opening the correct file path. If Python cannot find the file at all, see ",[310,1460,1462],{"href":1461},"\u002Ferrors\u002Ffilenotfounderror-errno-2-no-such-file-or-directory-fix\u002F","FileNotFoundError: No such file or directory",[96,1464,1466],{"id":1465},"what-not-to-do","What not to do",[20,1468,1469],{},"Common mistakes:",[72,1471,1472,1478,1481,1484],{},[75,1473,1474,1475,1477],{},"Do not use ",[15,1476,727],{}," as your first fix if the text matters.",[75,1479,1480],{},"Do not guess random encodings without checking the data source.",[75,1482,1483],{},"Do not open binary files like images, PDFs, or Excel files in text mode.",[75,1485,1486],{},"Do not assume every CSV file is UTF-8.",[96,1488,1490],{"id":1489},"common-causes","Common causes",[20,1492,1493],{},"Here are the most common reasons for this error:",[72,1495,1496,1504,1507,1510,1513],{},[75,1497,1498,1499,728,1501,1503],{},"The file was saved in ",[15,1500,251],{},[15,1502,149],{},", not UTF-8.",[75,1505,1506],{},"The file contains mixed or damaged text encoding.",[75,1508,1509],{},"A binary file was opened as if it were a text file.",[75,1511,1512],{},"Bytes from an API or download were decoded with the wrong encoding.",[75,1514,1515],{},"A CSV file came from Excel or another tool using a non-UTF-8 encoding.",[96,1517,1519],{"id":1518},"faq","FAQ",[459,1521,1523],{"id":1522},"why-does-python-try-utf-8-by-default","Why does Python try UTF-8 by default?",[20,1525,1526],{},"UTF-8 is a common text encoding and is the default in many Python setups and tools.",[459,1528,1530,1531,1533],{"id":1529},"should-i-use-errorsignore-to-fix-this","Should I use ",[15,1532,727],{}," to fix this?",[20,1535,1536],{},"Only if losing some characters is acceptable. It hides the problem and can remove important text.",[459,1538,1540,1541,314,1543,1545],{"id":1539},"what-is-the-difference-between-latin-1-and-cp1252","What is the difference between ",[15,1542,149],{},[15,1544,251],{},"?",[20,1547,1548,1549,1551,1552,1554],{},"They are similar single-byte encodings, but ",[15,1550,251],{}," supports some extra printable characters where ",[15,1553,149],{}," has control codes.",[459,1556,1558],{"id":1557},"why-does-this-happen-more-with-csv-files-from-excel","Why does this happen more with CSV files from Excel?",[20,1560,1561,1562,1564],{},"Some CSV exports use encodings like ",[15,1563,251],{}," instead of UTF-8, especially on Windows.",[96,1566,1568],{"id":1567},"see-also","See also",[72,1570,1571,1575,1580,1585,1590],{},[75,1572,1573],{},[310,1574,632],{"href":631},[75,1576,1577],{},[310,1578,1579],{"href":312},"How to read a file in Python",[75,1581,1582],{},[310,1583,1584],{"href":1166},"How to read a CSV file in Python",[75,1586,1587],{},[310,1588,1589],{"href":317},"Python file handling basics: read and write",[75,1591,1592],{},[310,1593,1595],{"href":1594},"\u002Ferrors\u002Funicodeencodeerror-in-python-causes-and-fixes\u002F","UnicodeEncodeError in Python: causes and fixes",[1597,1598,1599],"style",{},"html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s99_P, html code.shiki .s99_P{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#E36209;--shiki-default-font-style:inherit;--shiki-dark:#FFAB70;--shiki-dark-font-style:inherit}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sMMDD, html code.shiki .sMMDD{--shiki-light:#90A4AE;--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":29,"searchDepth":167,"depth":167,"links":1601},[1602,1603,1604,1605,1606,1614,1615,1624,1625,1626,1634],{"id":98,"depth":167,"text":99},{"id":211,"depth":167,"text":212},{"id":278,"depth":167,"text":279},{"id":321,"depth":167,"text":322},{"id":456,"depth":167,"text":457,"children":1607},[1608,1609,1611,1613],{"id":461,"depth":187,"text":462},{"id":635,"depth":187,"text":1610},"If the file has a UTF-8 BOM, try utf-8-sig",{"id":723,"depth":187,"text":1612},"Use errors='ignore' or errors='replace' only if needed",{"id":942,"depth":187,"text":943},{"id":1115,"depth":167,"text":1116},{"id":1170,"depth":167,"text":1171,"children":1616},[1617,1618,1620,1622],{"id":1177,"depth":187,"text":1178},{"id":1256,"depth":187,"text":1619},"2. Try utf-8-sig",{"id":1324,"depth":187,"text":1621},"3. Try cp1252",{"id":1391,"depth":187,"text":1623},"4. Try latin-1",{"id":1465,"depth":167,"text":1466},{"id":1489,"depth":167,"text":1490},{"id":1518,"depth":167,"text":1519,"children":1627},[1628,1629,1631,1633],{"id":1522,"depth":187,"text":1523},{"id":1529,"depth":187,"text":1630},"Should I use errors='ignore' to fix this?",{"id":1539,"depth":187,"text":1632},"What is the difference between latin-1 and cp1252?",{"id":1557,"depth":187,"text":1558},{"id":1567,"depth":167,"text":1568},"Master unicodedecodeerror utf 8 codec cant decode byte fix in our comprehensive Python beginner guide.","md",{},"\u002Ferrors\u002Funicodedecodeerror-utf-8-codec-cant-decode-byte-fix",{"title":5,"description":1635},"errors\u002Funicodedecodeerror-utf-8-codec-cant-decode-byte-fix","lNmITUUU1_F2-DgT3VtNktjPx_DnxLxbJoRxAzGeLmA",1777585509127]