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