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