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