[{"data":1,"prerenderedAt":1905},["ShallowReactive",2],{"doc-\u002Fexamples\u002Fpython-csv-writer-example":3},{"id":4,"title":5,"body":6,"description":1898,"extension":1899,"meta":1900,"navigation":61,"path":1901,"seo":1902,"stem":1903,"__hash__":1904},"content\u002Fexamples\u002Fpython-csv-writer-example.md","Python CSV Writer Example",{"type":7,"value":8,"toc":1859},"minimark",[9,13,22,29,34,344,355,359,380,383,387,390,620,627,638,644,650,654,771,778,789,793,848,851,882,894,898,922,925,928,932,951,957,960,964,970,978,981,995,998,1013,1016,1036,1040,1044,1054,1238,1252,1259,1263,1266,1322,1326,1329,1423,1427,1433,1447,1454,1458,1461,1475,1482,1486,1489,1495,1504,1507,1517,1520,1541,1547,1553,1556,1604,1608,1614,1620,1624,1627,1680,1683,1725,1728,1732,1735,1746,1749,1753,1756,1778,1782,1792,1797,1802,1809,1812,1816,1823,1827,1833,1837,1855],[10,11,5],"h1",{"id":12},"python-csv-writer-example",[14,15,16,17,21],"p",{},"This example shows how to write rows to a CSV file in Python with the built-in ",[18,19,20],"code",{},"csv"," module.",[14,23,24,25,28],{},"You will create a simple file named ",[18,26,27],{},"people.csv",", write a header row and some data rows, and see what the finished file looks like. This is a good starting point if you want to export table-like data from a Python script.",[30,31,33],"h2",{"id":32},"quick-example","Quick example",[35,36,41],"pre",{"className":37,"code":38,"language":39,"meta":40,"style":40},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","import csv\n\nrows = [\n    [\"name\", \"age\", \"city\"],\n    [\"Alice\", 25, \"London\"],\n    [\"Bob\", 30, \"Paris\"],\n    [\"Charlie\", 22, \"Berlin\"]\n]\n\nwith open(\"people.csv\", \"w\", newline=\"\", encoding=\"utf-8\") as file:\n    writer = csv.writer(file)\n    writer.writerows(rows)\n\nprint(\"CSV file written successfully\")\n","python","",[18,42,43,56,63,77,116,145,173,202,207,212,278,304,322,327],{"__ignoreMap":40},[44,45,48,52],"span",{"class":46,"line":47},"line",1,[44,49,51],{"class":50},"sVHd0","import",[44,53,55],{"class":54},"su5hD"," csv\n",[44,57,59],{"class":46,"line":58},2,[44,60,62],{"emptyLinePlaceholder":61},true,"\n",[44,64,66,69,73],{"class":46,"line":65},3,[44,67,68],{"class":54},"rows ",[44,70,72],{"class":71},"smGrS","=",[44,74,76],{"class":75},"sP7_E"," [\n",[44,78,80,83,87,91,93,96,99,102,104,106,108,111,113],{"class":46,"line":79},4,[44,81,82],{"class":75},"    [",[44,84,86],{"class":85},"sjJ54","\"",[44,88,90],{"class":89},"s_sjI","name",[44,92,86],{"class":85},[44,94,95],{"class":75},",",[44,97,98],{"class":85}," \"",[44,100,101],{"class":89},"age",[44,103,86],{"class":85},[44,105,95],{"class":75},[44,107,98],{"class":85},[44,109,110],{"class":89},"city",[44,112,86],{"class":85},[44,114,115],{"class":75},"],\n",[44,117,119,121,123,126,128,130,134,136,138,141,143],{"class":46,"line":118},5,[44,120,82],{"class":75},[44,122,86],{"class":85},[44,124,125],{"class":89},"Alice",[44,127,86],{"class":85},[44,129,95],{"class":75},[44,131,133],{"class":132},"srdBf"," 25",[44,135,95],{"class":75},[44,137,98],{"class":85},[44,139,140],{"class":89},"London",[44,142,86],{"class":85},[44,144,115],{"class":75},[44,146,148,150,152,155,157,159,162,164,166,169,171],{"class":46,"line":147},6,[44,149,82],{"class":75},[44,151,86],{"class":85},[44,153,154],{"class":89},"Bob",[44,156,86],{"class":85},[44,158,95],{"class":75},[44,160,161],{"class":132}," 30",[44,163,95],{"class":75},[44,165,98],{"class":85},[44,167,168],{"class":89},"Paris",[44,170,86],{"class":85},[44,172,115],{"class":75},[44,174,176,178,180,183,185,187,190,192,194,197,199],{"class":46,"line":175},7,[44,177,82],{"class":75},[44,179,86],{"class":85},[44,181,182],{"class":89},"Charlie",[44,184,86],{"class":85},[44,186,95],{"class":75},[44,188,189],{"class":132}," 22",[44,191,95],{"class":75},[44,193,98],{"class":85},[44,195,196],{"class":89},"Berlin",[44,198,86],{"class":85},[44,200,201],{"class":75},"]\n",[44,203,205],{"class":46,"line":204},8,[44,206,201],{"class":75},[44,208,210],{"class":46,"line":209},9,[44,211,62],{"emptyLinePlaceholder":61},[44,213,215,218,222,225,227,229,231,233,235,238,240,242,246,248,251,253,256,258,260,263,265,268,271,275],{"class":46,"line":214},10,[44,216,217],{"class":50},"with",[44,219,221],{"class":220},"sptTA"," open",[44,223,224],{"class":75},"(",[44,226,86],{"class":85},[44,228,27],{"class":89},[44,230,86],{"class":85},[44,232,95],{"class":75},[44,234,98],{"class":85},[44,236,237],{"class":89},"w",[44,239,86],{"class":85},[44,241,95],{"class":75},[44,243,245],{"class":244},"s99_P"," newline",[44,247,72],{"class":71},[44,249,250],{"class":85},"\"\"",[44,252,95],{"class":75},[44,254,255],{"class":244}," encoding",[44,257,72],{"class":71},[44,259,86],{"class":85},[44,261,262],{"class":89},"utf-8",[44,264,86],{"class":85},[44,266,267],{"class":75},")",[44,269,270],{"class":50}," as",[44,272,274],{"class":273},"sMMDD"," file",[44,276,277],{"class":75},":\n",[44,279,281,284,286,289,292,296,298,301],{"class":46,"line":280},11,[44,282,283],{"class":54},"    writer ",[44,285,72],{"class":71},[44,287,288],{"class":54}," csv",[44,290,291],{"class":75},".",[44,293,295],{"class":294},"slqww","writer",[44,297,224],{"class":75},[44,299,300],{"class":273},"file",[44,302,303],{"class":75},")\n",[44,305,307,310,312,315,317,320],{"class":46,"line":306},12,[44,308,309],{"class":54},"    writer",[44,311,291],{"class":75},[44,313,314],{"class":294},"writerows",[44,316,224],{"class":75},[44,318,319],{"class":294},"rows",[44,321,303],{"class":75},[44,323,325],{"class":46,"line":324},13,[44,326,62],{"emptyLinePlaceholder":61},[44,328,330,333,335,337,340,342],{"class":46,"line":329},14,[44,331,332],{"class":220},"print",[44,334,224],{"class":75},[44,336,86],{"class":85},[44,338,339],{"class":89},"CSV file written successfully",[44,341,86],{"class":85},[44,343,303],{"class":75},[14,345,346,350,351,354],{},[347,348,349],"strong",{},"Note:"," Use ",[18,352,353],{},"newline=\"\""," when opening the file. This helps avoid blank lines in CSV output on some systems.",[30,356,358],{"id":357},"what-this-example-does","What this example does",[360,361,362,368,371,377],"ul",{},[363,364,365,366],"li",{},"Creates a new CSV file named ",[18,367,27],{},[363,369,370],{},"Writes a header row and several data rows",[363,372,373,374,376],{},"Uses Python's built-in ",[18,375,20],{}," module",[363,378,379],{},"Shows a simple beginner-friendly way to generate CSV output",[14,381,382],{},"If you want a broader explanation of CSV tools in Python, see the Python csv module overview.",[30,384,386],{"id":385},"how-the-code-works","How the code works",[14,388,389],{},"Here is the same code again:",[35,391,392],{"className":37,"code":38,"language":39,"meta":40,"style":40},[18,393,394,400,404,412,440,464,488,512,516,520,570,588,602,606],{"__ignoreMap":40},[44,395,396,398],{"class":46,"line":47},[44,397,51],{"class":50},[44,399,55],{"class":54},[44,401,402],{"class":46,"line":58},[44,403,62],{"emptyLinePlaceholder":61},[44,405,406,408,410],{"class":46,"line":65},[44,407,68],{"class":54},[44,409,72],{"class":71},[44,411,76],{"class":75},[44,413,414,416,418,420,422,424,426,428,430,432,434,436,438],{"class":46,"line":79},[44,415,82],{"class":75},[44,417,86],{"class":85},[44,419,90],{"class":89},[44,421,86],{"class":85},[44,423,95],{"class":75},[44,425,98],{"class":85},[44,427,101],{"class":89},[44,429,86],{"class":85},[44,431,95],{"class":75},[44,433,98],{"class":85},[44,435,110],{"class":89},[44,437,86],{"class":85},[44,439,115],{"class":75},[44,441,442,444,446,448,450,452,454,456,458,460,462],{"class":46,"line":118},[44,443,82],{"class":75},[44,445,86],{"class":85},[44,447,125],{"class":89},[44,449,86],{"class":85},[44,451,95],{"class":75},[44,453,133],{"class":132},[44,455,95],{"class":75},[44,457,98],{"class":85},[44,459,140],{"class":89},[44,461,86],{"class":85},[44,463,115],{"class":75},[44,465,466,468,470,472,474,476,478,480,482,484,486],{"class":46,"line":147},[44,467,82],{"class":75},[44,469,86],{"class":85},[44,471,154],{"class":89},[44,473,86],{"class":85},[44,475,95],{"class":75},[44,477,161],{"class":132},[44,479,95],{"class":75},[44,481,98],{"class":85},[44,483,168],{"class":89},[44,485,86],{"class":85},[44,487,115],{"class":75},[44,489,490,492,494,496,498,500,502,504,506,508,510],{"class":46,"line":175},[44,491,82],{"class":75},[44,493,86],{"class":85},[44,495,182],{"class":89},[44,497,86],{"class":85},[44,499,95],{"class":75},[44,501,189],{"class":132},[44,503,95],{"class":75},[44,505,98],{"class":85},[44,507,196],{"class":89},[44,509,86],{"class":85},[44,511,201],{"class":75},[44,513,514],{"class":46,"line":204},[44,515,201],{"class":75},[44,517,518],{"class":46,"line":209},[44,519,62],{"emptyLinePlaceholder":61},[44,521,522,524,526,528,530,532,534,536,538,540,542,544,546,548,550,552,554,556,558,560,562,564,566,568],{"class":46,"line":214},[44,523,217],{"class":50},[44,525,221],{"class":220},[44,527,224],{"class":75},[44,529,86],{"class":85},[44,531,27],{"class":89},[44,533,86],{"class":85},[44,535,95],{"class":75},[44,537,98],{"class":85},[44,539,237],{"class":89},[44,541,86],{"class":85},[44,543,95],{"class":75},[44,545,245],{"class":244},[44,547,72],{"class":71},[44,549,250],{"class":85},[44,551,95],{"class":75},[44,553,255],{"class":244},[44,555,72],{"class":71},[44,557,86],{"class":85},[44,559,262],{"class":89},[44,561,86],{"class":85},[44,563,267],{"class":75},[44,565,270],{"class":50},[44,567,274],{"class":273},[44,569,277],{"class":75},[44,571,572,574,576,578,580,582,584,586],{"class":46,"line":280},[44,573,283],{"class":54},[44,575,72],{"class":71},[44,577,288],{"class":54},[44,579,291],{"class":75},[44,581,295],{"class":294},[44,583,224],{"class":75},[44,585,300],{"class":273},[44,587,303],{"class":75},[44,589,590,592,594,596,598,600],{"class":46,"line":306},[44,591,309],{"class":54},[44,593,291],{"class":75},[44,595,314],{"class":294},[44,597,224],{"class":75},[44,599,319],{"class":294},[44,601,303],{"class":75},[44,603,604],{"class":46,"line":324},[44,605,62],{"emptyLinePlaceholder":61},[44,607,608,610,612,614,616,618],{"class":46,"line":329},[44,609,332],{"class":220},[44,611,224],{"class":75},[44,613,86],{"class":85},[44,615,339],{"class":89},[44,617,86],{"class":85},[44,619,303],{"class":75},[621,622,624,625],"h3",{"id":623},"_1-import-csv","1. Import ",[18,626,20],{},[35,628,630],{"className":37,"code":629,"language":39,"meta":40,"style":40},"import csv\n",[18,631,632],{"__ignoreMap":40},[44,633,634,636],{"class":46,"line":47},[44,635,51],{"class":50},[44,637,55],{"class":54},[14,639,640,641,291],{},"This imports Python's built-in CSV module so you can use ",[18,642,643],{},"csv.writer()",[14,645,646,647,649],{},"If you forget this line, Python will not know what ",[18,648,20],{}," means.",[621,651,653],{"id":652},"_2-store-the-data-as-rows","2. Store the data as rows",[35,655,657],{"className":37,"code":656,"language":39,"meta":40,"style":40},"rows = [\n    [\"name\", \"age\", \"city\"],\n    [\"Alice\", 25, \"London\"],\n    [\"Bob\", 30, \"Paris\"],\n    [\"Charlie\", 22, \"Berlin\"]\n]\n",[18,658,659,667,695,719,743,767],{"__ignoreMap":40},[44,660,661,663,665],{"class":46,"line":47},[44,662,68],{"class":54},[44,664,72],{"class":71},[44,666,76],{"class":75},[44,668,669,671,673,675,677,679,681,683,685,687,689,691,693],{"class":46,"line":58},[44,670,82],{"class":75},[44,672,86],{"class":85},[44,674,90],{"class":89},[44,676,86],{"class":85},[44,678,95],{"class":75},[44,680,98],{"class":85},[44,682,101],{"class":89},[44,684,86],{"class":85},[44,686,95],{"class":75},[44,688,98],{"class":85},[44,690,110],{"class":89},[44,692,86],{"class":85},[44,694,115],{"class":75},[44,696,697,699,701,703,705,707,709,711,713,715,717],{"class":46,"line":65},[44,698,82],{"class":75},[44,700,86],{"class":85},[44,702,125],{"class":89},[44,704,86],{"class":85},[44,706,95],{"class":75},[44,708,133],{"class":132},[44,710,95],{"class":75},[44,712,98],{"class":85},[44,714,140],{"class":89},[44,716,86],{"class":85},[44,718,115],{"class":75},[44,720,721,723,725,727,729,731,733,735,737,739,741],{"class":46,"line":79},[44,722,82],{"class":75},[44,724,86],{"class":85},[44,726,154],{"class":89},[44,728,86],{"class":85},[44,730,95],{"class":75},[44,732,161],{"class":132},[44,734,95],{"class":75},[44,736,98],{"class":85},[44,738,168],{"class":89},[44,740,86],{"class":85},[44,742,115],{"class":75},[44,744,745,747,749,751,753,755,757,759,761,763,765],{"class":46,"line":118},[44,746,82],{"class":75},[44,748,86],{"class":85},[44,750,182],{"class":89},[44,752,86],{"class":85},[44,754,95],{"class":75},[44,756,189],{"class":132},[44,758,95],{"class":75},[44,760,98],{"class":85},[44,762,196],{"class":89},[44,764,86],{"class":85},[44,766,201],{"class":75},[44,768,769],{"class":46,"line":147},[44,770,201],{"class":75},[14,772,773,774,777],{},"This is a ",[347,775,776],{},"list of lists",":",[360,779,780,783,786],{},[363,781,782],{},"Each inner list is one row",[363,784,785],{},"The first row is the header",[363,787,788],{},"The remaining rows are the data",[621,790,792],{"id":791},"_3-open-the-file-in-write-mode","3. Open the file in write mode",[35,794,796],{"className":37,"code":795,"language":39,"meta":40,"style":40},"with open(\"people.csv\", \"w\", newline=\"\", encoding=\"utf-8\") as file:\n",[18,797,798],{"__ignoreMap":40},[44,799,800,802,804,806,808,810,812,814,816,818,820,822,824,826,828,830,832,834,836,838,840,842,844,846],{"class":46,"line":47},[44,801,217],{"class":50},[44,803,221],{"class":220},[44,805,224],{"class":75},[44,807,86],{"class":85},[44,809,27],{"class":89},[44,811,86],{"class":85},[44,813,95],{"class":75},[44,815,98],{"class":85},[44,817,237],{"class":89},[44,819,86],{"class":85},[44,821,95],{"class":75},[44,823,245],{"class":244},[44,825,72],{"class":71},[44,827,250],{"class":85},[44,829,95],{"class":75},[44,831,255],{"class":244},[44,833,72],{"class":71},[44,835,86],{"class":85},[44,837,262],{"class":89},[44,839,86],{"class":85},[44,841,267],{"class":75},[44,843,270],{"class":50},[44,845,274],{"class":273},[44,847,277],{"class":75},[14,849,850],{},"This line does several important things:",[360,852,853,859,865,870,876],{},[363,854,855,858],{},[18,856,857],{},"\"people.csv\""," is the file name",[363,860,861,864],{},[18,862,863],{},"\"w\""," means write mode",[363,866,867,869],{},[18,868,353],{}," helps prevent extra blank lines",[363,871,872,875],{},[18,873,874],{},"encoding=\"utf-8\""," is a good default for text files",[363,877,878,881],{},[18,879,880],{},"with open(...)"," automatically closes the file when done",[14,883,884,885,888,889,291],{},"If you are new to ",[18,886,887],{},"open()",", see ",[890,891,893],"a",{"href":892},"\u002Freference\u002Fpython-open-function-explained\u002F","Python open() function explained",[621,895,897],{"id":896},"_4-create-a-csv-writer","4. Create a CSV writer",[35,899,901],{"className":37,"code":900,"language":39,"meta":40,"style":40},"writer = csv.writer(file)\n",[18,902,903],{"__ignoreMap":40},[44,904,905,908,910,912,914,916,918,920],{"class":46,"line":47},[44,906,907],{"class":54},"writer ",[44,909,72],{"class":71},[44,911,288],{"class":54},[44,913,291],{"class":75},[44,915,295],{"class":294},[44,917,224],{"class":75},[44,919,300],{"class":273},[44,921,303],{"class":75},[14,923,924],{},"This creates a writer object connected to the file.",[14,926,927],{},"You use this writer to send rows into the CSV file.",[621,929,931],{"id":930},"_5-write-all-rows-at-once","5. Write all rows at once",[35,933,935],{"className":37,"code":934,"language":39,"meta":40,"style":40},"writer.writerows(rows)\n",[18,936,937],{"__ignoreMap":40},[44,938,939,941,943,945,947,949],{"class":46,"line":47},[44,940,295],{"class":54},[44,942,291],{"class":75},[44,944,314],{"class":294},[44,946,224],{"class":75},[44,948,319],{"class":294},[44,950,303],{"class":75},[14,952,953,954,956],{},"This writes every row from the ",[18,955,319],{}," list into the file.",[14,958,959],{},"After that, the file is ready to use.",[30,961,963],{"id":962},"expected-file-content","Expected file content",[14,965,966,967,969],{},"After running the script, the file ",[18,968,27],{}," should contain:",[35,971,976],{"className":972,"code":974,"language":975,"meta":40},[973],"language-text","name,age,city\nAlice,25,London\nBob,30,Paris\nCharlie,22,Berlin\n","text",[18,977,974],{"__ignoreMap":40},[14,979,980],{},"You can open this file in:",[360,982,983,986,989,992],{},[363,984,985],{},"A text editor",[363,987,988],{},"Excel",[363,990,991],{},"Google Sheets",[363,993,994],{},"Other spreadsheet programs",[14,996,997],{},"To run the script, you can use:",[35,999,1003],{"className":1000,"code":1001,"language":1002,"meta":40,"style":40},"language-bash shiki shiki-themes material-theme-lighter github-light github-dark","python your_script.py\n","bash",[18,1004,1005],{"__ignoreMap":40},[44,1006,1007,1010],{"class":46,"line":47},[44,1008,39],{"class":1009},"sbgvK",[44,1011,1012],{"class":89}," your_script.py\n",[14,1014,1015],{},"Then check the file content with one of these commands:",[35,1017,1019],{"className":1000,"code":1018,"language":1002,"meta":40,"style":40},"type people.csv\ncat people.csv\n",[18,1020,1021,1029],{"__ignoreMap":40},[44,1022,1023,1026],{"class":46,"line":47},[44,1024,1025],{"class":220},"type",[44,1027,1028],{"class":89}," people.csv\n",[44,1030,1031,1034],{"class":46,"line":58},[44,1032,1033],{"class":1009},"cat",[44,1035,1028],{"class":89},[30,1037,1039],{"id":1038},"useful-beginner-variations","Useful beginner variations",[621,1041,1043],{"id":1042},"write-one-row-at-a-time","Write one row at a time",[14,1045,1046,1047,1050,1051,291],{},"If you want to write rows one by one, use ",[18,1048,1049],{},"writer.writerow(...)"," instead of ",[18,1052,1053],{},"writer.writerows(...)",[35,1055,1057],{"className":37,"code":1056,"language":39,"meta":40,"style":40},"import csv\n\nwith open(\"people.csv\", \"w\", newline=\"\", encoding=\"utf-8\") as file:\n    writer = csv.writer(file)\n\n    writer.writerow([\"name\", \"age\", \"city\"])\n    writer.writerow([\"Alice\", 25, \"London\"])\n    writer.writerow([\"Bob\", 30, \"Paris\"])\n",[18,1058,1059,1065,1069,1119,1137,1141,1178,1208],{"__ignoreMap":40},[44,1060,1061,1063],{"class":46,"line":47},[44,1062,51],{"class":50},[44,1064,55],{"class":54},[44,1066,1067],{"class":46,"line":58},[44,1068,62],{"emptyLinePlaceholder":61},[44,1070,1071,1073,1075,1077,1079,1081,1083,1085,1087,1089,1091,1093,1095,1097,1099,1101,1103,1105,1107,1109,1111,1113,1115,1117],{"class":46,"line":65},[44,1072,217],{"class":50},[44,1074,221],{"class":220},[44,1076,224],{"class":75},[44,1078,86],{"class":85},[44,1080,27],{"class":89},[44,1082,86],{"class":85},[44,1084,95],{"class":75},[44,1086,98],{"class":85},[44,1088,237],{"class":89},[44,1090,86],{"class":85},[44,1092,95],{"class":75},[44,1094,245],{"class":244},[44,1096,72],{"class":71},[44,1098,250],{"class":85},[44,1100,95],{"class":75},[44,1102,255],{"class":244},[44,1104,72],{"class":71},[44,1106,86],{"class":85},[44,1108,262],{"class":89},[44,1110,86],{"class":85},[44,1112,267],{"class":75},[44,1114,270],{"class":50},[44,1116,274],{"class":273},[44,1118,277],{"class":75},[44,1120,1121,1123,1125,1127,1129,1131,1133,1135],{"class":46,"line":79},[44,1122,283],{"class":54},[44,1124,72],{"class":71},[44,1126,288],{"class":54},[44,1128,291],{"class":75},[44,1130,295],{"class":294},[44,1132,224],{"class":75},[44,1134,300],{"class":273},[44,1136,303],{"class":75},[44,1138,1139],{"class":46,"line":118},[44,1140,62],{"emptyLinePlaceholder":61},[44,1142,1143,1145,1147,1150,1153,1155,1157,1159,1161,1163,1165,1167,1169,1171,1173,1175],{"class":46,"line":147},[44,1144,309],{"class":54},[44,1146,291],{"class":75},[44,1148,1149],{"class":294},"writerow",[44,1151,1152],{"class":75},"([",[44,1154,86],{"class":85},[44,1156,90],{"class":89},[44,1158,86],{"class":85},[44,1160,95],{"class":75},[44,1162,98],{"class":85},[44,1164,101],{"class":89},[44,1166,86],{"class":85},[44,1168,95],{"class":75},[44,1170,98],{"class":85},[44,1172,110],{"class":89},[44,1174,86],{"class":85},[44,1176,1177],{"class":75},"])\n",[44,1179,1180,1182,1184,1186,1188,1190,1192,1194,1196,1198,1200,1202,1204,1206],{"class":46,"line":175},[44,1181,309],{"class":54},[44,1183,291],{"class":75},[44,1185,1149],{"class":294},[44,1187,1152],{"class":75},[44,1189,86],{"class":85},[44,1191,125],{"class":89},[44,1193,86],{"class":85},[44,1195,95],{"class":75},[44,1197,133],{"class":132},[44,1199,95],{"class":75},[44,1201,98],{"class":85},[44,1203,140],{"class":89},[44,1205,86],{"class":85},[44,1207,1177],{"class":75},[44,1209,1210,1212,1214,1216,1218,1220,1222,1224,1226,1228,1230,1232,1234,1236],{"class":46,"line":204},[44,1211,309],{"class":54},[44,1213,291],{"class":75},[44,1215,1149],{"class":294},[44,1217,1152],{"class":75},[44,1219,86],{"class":85},[44,1221,154],{"class":89},[44,1223,86],{"class":85},[44,1225,95],{"class":75},[44,1227,161],{"class":132},[44,1229,95],{"class":75},[44,1231,98],{"class":85},[44,1233,168],{"class":89},[44,1235,86],{"class":85},[44,1237,1177],{"class":75},[360,1239,1240,1246],{},[363,1241,1242,1245],{},[18,1243,1244],{},"writerow()"," writes one row",[363,1247,1248,1251],{},[18,1249,1250],{},"writerows()"," writes many rows",[14,1253,1254,1255,291],{},"If you want a step-by-step guide, see ",[890,1256,1258],{"href":1257},"\u002Fhow-to\u002Fhow-to-write-a-csv-file-in-python\u002F","How to write a CSV file in Python",[621,1260,1262],{"id":1261},"change-the-file-name","Change the file name",[14,1264,1265],{},"You can create a different CSV file by changing the file name:",[35,1267,1269],{"className":37,"code":1268,"language":39,"meta":40,"style":40},"with open(\"students.csv\", \"w\", newline=\"\", encoding=\"utf-8\") as file:\n",[18,1270,1271],{"__ignoreMap":40},[44,1272,1273,1275,1277,1279,1281,1284,1286,1288,1290,1292,1294,1296,1298,1300,1302,1304,1306,1308,1310,1312,1314,1316,1318,1320],{"class":46,"line":47},[44,1274,217],{"class":50},[44,1276,221],{"class":220},[44,1278,224],{"class":75},[44,1280,86],{"class":85},[44,1282,1283],{"class":89},"students.csv",[44,1285,86],{"class":85},[44,1287,95],{"class":75},[44,1289,98],{"class":85},[44,1291,237],{"class":89},[44,1293,86],{"class":85},[44,1295,95],{"class":75},[44,1297,245],{"class":244},[44,1299,72],{"class":71},[44,1301,250],{"class":85},[44,1303,95],{"class":75},[44,1305,255],{"class":244},[44,1307,72],{"class":71},[44,1309,86],{"class":85},[44,1311,262],{"class":89},[44,1313,86],{"class":85},[44,1315,267],{"class":75},[44,1317,270],{"class":50},[44,1319,274],{"class":273},[44,1321,277],{"class":75},[621,1323,1325],{"id":1324},"replace-the-example-data","Replace the example data",[14,1327,1328],{},"You can swap the sample rows with your own data:",[35,1330,1332],{"className":37,"code":1331,"language":39,"meta":40,"style":40},"rows = [\n    [\"product\", \"price\", \"stock\"],\n    [\"Pen\", 1.5, 100],\n    [\"Notebook\", 3.0, 50]\n]\n",[18,1333,1334,1342,1373,1396,1419],{"__ignoreMap":40},[44,1335,1336,1338,1340],{"class":46,"line":47},[44,1337,68],{"class":54},[44,1339,72],{"class":71},[44,1341,76],{"class":75},[44,1343,1344,1346,1348,1351,1353,1355,1357,1360,1362,1364,1366,1369,1371],{"class":46,"line":58},[44,1345,82],{"class":75},[44,1347,86],{"class":85},[44,1349,1350],{"class":89},"product",[44,1352,86],{"class":85},[44,1354,95],{"class":75},[44,1356,98],{"class":85},[44,1358,1359],{"class":89},"price",[44,1361,86],{"class":85},[44,1363,95],{"class":75},[44,1365,98],{"class":85},[44,1367,1368],{"class":89},"stock",[44,1370,86],{"class":85},[44,1372,115],{"class":75},[44,1374,1375,1377,1379,1382,1384,1386,1389,1391,1394],{"class":46,"line":65},[44,1376,82],{"class":75},[44,1378,86],{"class":85},[44,1380,1381],{"class":89},"Pen",[44,1383,86],{"class":85},[44,1385,95],{"class":75},[44,1387,1388],{"class":132}," 1.5",[44,1390,95],{"class":75},[44,1392,1393],{"class":132}," 100",[44,1395,115],{"class":75},[44,1397,1398,1400,1402,1405,1407,1409,1412,1414,1417],{"class":46,"line":79},[44,1399,82],{"class":75},[44,1401,86],{"class":85},[44,1403,1404],{"class":89},"Notebook",[44,1406,86],{"class":85},[44,1408,95],{"class":75},[44,1410,1411],{"class":132}," 3.0",[44,1413,95],{"class":75},[44,1415,1416],{"class":132}," 50",[44,1418,201],{"class":75},[44,1420,1421],{"class":46,"line":118},[44,1422,201],{"class":75},[621,1424,1426],{"id":1425},"write-data-from-another-part-of-your-program","Write data from another part of your program",[14,1428,1429,1430,1432],{},"You can build the ",[18,1431,319],{}," list from:",[360,1434,1435,1438,1441,1444],{},[363,1436,1437],{},"User input",[363,1439,1440],{},"Calculations",[363,1442,1443],{},"API results",[363,1445,1446],{},"Data read from another file",[14,1448,1449,1450,291],{},"If you want to add new rows without replacing the old file, see ",[890,1451,1453],{"href":1452},"\u002Fhow-to\u002Fhow-to-append-to-a-file-in-python\u002F","How to append to a file in Python",[30,1455,1457],{"id":1456},"when-this-example-is-useful","When this example is useful",[14,1459,1460],{},"This example is helpful when you need to:",[360,1462,1463,1466,1469,1472],{},[363,1464,1465],{},"Save simple table data to a file",[363,1467,1468],{},"Export results from a Python script",[363,1470,1471],{},"Create spreadsheet-friendly output",[363,1473,1474],{},"Learn the difference between plain text files and structured CSV files",[14,1476,1477,1478,291],{},"A matching next step is reading CSV files back into Python. For that, see the ",[890,1479,1481],{"href":1480},"\u002Fexamples\u002Fpython-csv-reader-example\u002F","Python CSV reader example",[30,1483,1485],{"id":1484},"common-mistakes","Common mistakes",[14,1487,1488],{},"Here are some common problems beginners run into.",[621,1490,1492,1493],{"id":1491},"forgetting-to-import-csv","Forgetting to import ",[18,1494,20],{},[14,1496,1497,1498,1500,1501,1503],{},"If you use ",[18,1499,643],{}," without importing ",[18,1502,20],{},", Python will raise an error.",[14,1505,1506],{},"Make sure this line is at the top:",[35,1508,1509],{"className":37,"code":629,"language":39,"meta":40,"style":40},[18,1510,1511],{"__ignoreMap":40},[44,1512,1513,1515],{"class":46,"line":47},[44,1514,51],{"class":50},[44,1516,55],{"class":54},[14,1518,1519],{},"You can quickly test the module with:",[35,1521,1523],{"className":1000,"code":1522,"language":1002,"meta":40,"style":40},"python -c \"import csv; print('csv module loaded')\"\n",[18,1524,1525],{"__ignoreMap":40},[44,1526,1527,1529,1533,1535,1538],{"class":46,"line":47},[44,1528,39],{"class":1009},[44,1530,1532],{"class":1531},"stzsN"," -c",[44,1534,98],{"class":85},[44,1536,1537],{"class":89},"import csv; print('csv module loaded')",[44,1539,1540],{"class":85},"\"\n",[621,1542,1544,1545],{"id":1543},"opening-the-file-without-newline","Opening the file without ",[18,1546,353],{},[14,1548,1549,1550,1552],{},"Without ",[18,1551,353],{},", some systems may create extra blank lines in the CSV output.",[14,1554,1555],{},"Use:",[35,1557,1559],{"className":37,"code":1558,"language":39,"meta":40,"style":40},"open(\"people.csv\", \"w\", newline=\"\", encoding=\"utf-8\")\n",[18,1560,1561],{"__ignoreMap":40},[44,1562,1563,1566,1568,1570,1572,1574,1576,1578,1580,1582,1584,1586,1588,1590,1592,1594,1596,1598,1600,1602],{"class":46,"line":47},[44,1564,1565],{"class":220},"open",[44,1567,224],{"class":75},[44,1569,86],{"class":85},[44,1571,27],{"class":89},[44,1573,86],{"class":85},[44,1575,95],{"class":75},[44,1577,98],{"class":85},[44,1579,237],{"class":89},[44,1581,86],{"class":85},[44,1583,95],{"class":75},[44,1585,245],{"class":244},[44,1587,72],{"class":71},[44,1589,250],{"class":85},[44,1591,95],{"class":75},[44,1593,255],{"class":244},[44,1595,72],{"class":71},[44,1597,86],{"class":85},[44,1599,262],{"class":89},[44,1601,86],{"class":85},[44,1603,303],{"class":75},[621,1605,1607],{"id":1606},"overwriting-an-existing-file-by-mistake","Overwriting an existing file by mistake",[14,1609,1610,1611,1613],{},"Using ",[18,1612,863],{}," replaces the old file content.",[14,1615,1616,1617,1619],{},"If ",[18,1618,27],{}," already exists, it will be overwritten.",[621,1621,1623],{"id":1622},"passing-the-wrong-data-shape","Passing the wrong data shape",[14,1625,1626],{},"This works:",[35,1628,1630],{"className":37,"code":1629,"language":39,"meta":40,"style":40},"rows = [\n    [\"name\", \"age\"],\n    [\"Alice\", 25]\n]\n",[18,1631,1632,1640,1660,1676],{"__ignoreMap":40},[44,1633,1634,1636,1638],{"class":46,"line":47},[44,1635,68],{"class":54},[44,1637,72],{"class":71},[44,1639,76],{"class":75},[44,1641,1642,1644,1646,1648,1650,1652,1654,1656,1658],{"class":46,"line":58},[44,1643,82],{"class":75},[44,1645,86],{"class":85},[44,1647,90],{"class":89},[44,1649,86],{"class":85},[44,1651,95],{"class":75},[44,1653,98],{"class":85},[44,1655,101],{"class":89},[44,1657,86],{"class":85},[44,1659,115],{"class":75},[44,1661,1662,1664,1666,1668,1670,1672,1674],{"class":46,"line":65},[44,1663,82],{"class":75},[44,1665,86],{"class":85},[44,1667,125],{"class":89},[44,1669,86],{"class":85},[44,1671,95],{"class":75},[44,1673,133],{"class":132},[44,1675,201],{"class":75},[44,1677,1678],{"class":46,"line":79},[44,1679,201],{"class":75},[14,1681,1682],{},"This is not a proper list of rows:",[35,1684,1686],{"className":37,"code":1685,"language":39,"meta":40,"style":40},"rows = [\"name\", \"age\", \"Alice\", 25]\n",[18,1687,1688],{"__ignoreMap":40},[44,1689,1690,1692,1694,1697,1699,1701,1703,1705,1707,1709,1711,1713,1715,1717,1719,1721,1723],{"class":46,"line":47},[44,1691,68],{"class":54},[44,1693,72],{"class":71},[44,1695,1696],{"class":75}," [",[44,1698,86],{"class":85},[44,1700,90],{"class":89},[44,1702,86],{"class":85},[44,1704,95],{"class":75},[44,1706,98],{"class":85},[44,1708,101],{"class":89},[44,1710,86],{"class":85},[44,1712,95],{"class":75},[44,1714,98],{"class":85},[44,1716,125],{"class":89},[44,1718,86],{"class":85},[44,1720,95],{"class":75},[44,1722,133],{"class":132},[44,1724,201],{"class":75},[14,1726,1727],{},"A CSV writer expects each row to be a separate list or iterable.",[621,1729,1731],{"id":1730},"trying-to-write-unsupported-custom-objects","Trying to write unsupported custom objects",[14,1733,1734],{},"CSV files work best with simple values such as:",[360,1736,1737,1740,1743],{},[363,1738,1739],{},"Strings",[363,1741,1742],{},"Integers",[363,1744,1745],{},"Floats",[14,1747,1748],{},"If you try to write custom objects directly, the output may not be what you expect. Convert values to simple text or numbers first.",[621,1750,1752],{"id":1751},"file-and-permission-problems","File and permission problems",[14,1754,1755],{},"If Python cannot create or write the file, you may see an error such as:",[360,1757,1758,1768],{},[363,1759,1760],{},[890,1761,1763,1764,1767],{"href":1762},"\u002Ferrors\u002Ffilenotfounderror-errno-2-no-such-file-or-directory-fix\u002F","FileNotFoundError: ",[44,1765,1766],{},"Errno 2"," No such file or directory",[363,1769,1770],{},[890,1771,1773,1774,1777],{"href":1772},"\u002Ferrors\u002Fpermissionerror-errno-13-permission-denied-fix\u002F","PermissionError: ",[44,1775,1776],{},"Errno 13"," Permission denied",[30,1779,1781],{"id":1780},"faq","FAQ",[621,1783,1785,1786,1788,1789,1791],{"id":1784},"what-is-the-difference-between-writerow-and-writerows","What is the difference between ",[18,1787,1244],{}," and ",[18,1790,1250],{},"?",[14,1793,1794,1796],{},[18,1795,1244],{}," writes one row.",[14,1798,1799,1801],{},[18,1800,1250],{}," writes multiple rows from a list or other iterable.",[621,1803,1805,1806,1808],{"id":1804},"why-use-newline-when-opening-a-csv-file","Why use ",[18,1807,353],{}," when opening a CSV file?",[14,1810,1811],{},"It prevents extra blank lines from appearing in the output on some operating systems.",[621,1813,1815],{"id":1814},"can-i-write-dictionaries-to-a-csv-file","Can I write dictionaries to a CSV file?",[14,1817,1818,1819,1822],{},"Yes, but that is usually done with ",[18,1820,1821],{},"csv.DictWriter",", which is better covered separately than in this basic example.",[621,1824,1826],{"id":1825},"will-this-overwrite-an-existing-file","Will this overwrite an existing file?",[14,1828,1829,1830,1832],{},"Yes. Opening a file with ",[18,1831,863],{}," replaces the old content. Use append mode if you want to add rows instead.",[30,1834,1836],{"id":1835},"see-also","See also",[360,1838,1839,1843,1847,1851],{},[363,1840,1841],{},[890,1842,1258],{"href":1257},[363,1844,1845],{},[890,1846,893],{"href":892},[363,1848,1849],{},[890,1850,1453],{"href":1452},[363,1852,1853],{},[890,1854,1481],{"href":1480},[1856,1857,1858],"style",{},"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 .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 .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 .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .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 .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":40,"searchDepth":58,"depth":58,"links":1860},[1861,1862,1863,1871,1872,1878,1879,1889,1897],{"id":32,"depth":58,"text":33},{"id":357,"depth":58,"text":358},{"id":385,"depth":58,"text":386,"children":1864},[1865,1867,1868,1869,1870],{"id":623,"depth":65,"text":1866},"1. Import csv",{"id":652,"depth":65,"text":653},{"id":791,"depth":65,"text":792},{"id":896,"depth":65,"text":897},{"id":930,"depth":65,"text":931},{"id":962,"depth":58,"text":963},{"id":1038,"depth":58,"text":1039,"children":1873},[1874,1875,1876,1877],{"id":1042,"depth":65,"text":1043},{"id":1261,"depth":65,"text":1262},{"id":1324,"depth":65,"text":1325},{"id":1425,"depth":65,"text":1426},{"id":1456,"depth":58,"text":1457},{"id":1484,"depth":58,"text":1485,"children":1880},[1881,1883,1885,1886,1887,1888],{"id":1491,"depth":65,"text":1882},"Forgetting to import csv",{"id":1543,"depth":65,"text":1884},"Opening the file without newline=\"\"",{"id":1606,"depth":65,"text":1607},{"id":1622,"depth":65,"text":1623},{"id":1730,"depth":65,"text":1731},{"id":1751,"depth":65,"text":1752},{"id":1780,"depth":58,"text":1781,"children":1890},[1891,1893,1895,1896],{"id":1784,"depth":65,"text":1892},"What is the difference between writerow() and writerows()?",{"id":1804,"depth":65,"text":1894},"Why use newline=\"\" when opening a CSV file?",{"id":1814,"depth":65,"text":1815},{"id":1825,"depth":65,"text":1826},{"id":1835,"depth":58,"text":1836},"Master python csv writer example in our comprehensive Python beginner guide.","md",{},"\u002Fexamples\u002Fpython-csv-writer-example",{"title":5,"description":1898},"examples\u002Fpython-csv-writer-example","heoZ5PcqN6LowKTgQeDWlp4x3f_Vi88qZN7nhRAW5zQ",1777585474980]