Python – Convert CSV to List of Lists - Finxter
文章推薦指數: 80 %
To read a CSV to a nested list in pure Python, open the file using open('my_file.csv') , read all lines into a variable using f.readlines() . Skiptocontent Menu 4.7/5-(4votes) TableofContents ProblemFormulationMethod1:csv.reader()Method2:One-LinerMethod3:PandasMethod4:RawPythonNoDependencyMorePythonCSVConversionsPythonOne-LinersBook:MastertheSingleLineFirst! ProblemFormulation GivenaCSVfile(e.g.,storedinthefilewithname'my_file.csv'). INPUT:file'my_file.csv' 9,8,7 6,5,4 3,2,1 Challenge:Howtoconvertittoalistoflists(=nestedlist),i.e.,puttingtherowvaluesintotheinnerlists? OUTPUT:Pythonlistoflists [[9,8,7],[6,5,4],[3,2,1]] Method1:csv.reader() Method1:csv.reader() ToconvertaCSVfile'my_file.csv'intoalistoflistsinPython,usethecsv.reader(file_obj)methodtocreateaCSVfilereader.Thenconverttheresultingobjecttoalistusingthelist()constructor. Here’sasimpleexamplethatconvertsourCSVfiletoanestedlistusingthisapproach: importcsv csv_filename='my_file.csv' withopen(csv_filename)asf: reader=csv.reader(f) lst=list(reader) Output: print(lst) #[['9','8','7'],['6','5','4'],['3','2','1']] Method2:One-Liner Method2:One-Liner YoucanalsoconvertaCSVtoalistoflistsusingthefollowingPythonone-linerthatopensthefileusingopen(),passesthefileobjectintothecsv.reader()function,andconverttheCSVreaderobjecttoalistusingthelist()built-infunctioninPython. Here’showthatlooks: importcsv;lst=list(csv.reader(open('my_file.csv')));print(lst) Concise,isn’tit?🙂 Theoutputisthesame: [['9','8','7'],['6','5','4'],['3','2','1']] Bytheway:Doyouneedallinnerelementsconvertedtoacertaindatatypesuchasinteger? Ifso,usethefollowingapproachontopofthenestedlistofstrings,lst,obtainedfromthepreviousmethods: new_lst=[[int(x)forxininner]forinnerinlst] Theconvertednew_lstnowcontainsanestedlistofints: print(new_lst) #[[9,8,7],[6,5,4],[3,2,1]] Method3:Pandas Method3:Pandas YoucanconvertaCSVtoalistoflistswithPandasbyfirstreadingtheCSVwithoutheaderlineusingpd.read_csv('my_file.csv',header=None)functionandsecondconvertingtheresultingDataFrametoanestedlistusingdf.values.tolist(). Here’sanexamplethatconvertstheCSVtoaPandasDataFrameandthentoanestedrawPythonlist: importpandasaspd df=pd.read_csv('my_file.csv',header=None) lst=df.values.tolist() print(lst) #[[9,8,7],[6,5,4],[3,2,1]] Thiswaseasy,wasn’tit?🙂 Ofcourse,youcanalsoone-linerizeitbychainingcommandslikeso: lst=pd.read_csv('my_file.csv',header=None).values.tolist() Method4:RawPythonNoDependency Method4:RawPythonNoDependency Ifyou’relikeme,youtrytoavoidusingdependenciesiftheyarenotneeded.RawPythonisoftenmoreefficientandsimpleenoughanyways.Also,youdon’topenyourselfupagainstunnecessaryrisksandcomplexities. Question:So,isthereasimplewaytoreadaCSVtoalistoflistsinrawPythonwithoutexternaldependencies? Sure! ToreadaCSVtoanestedlistinpurePython,openthefileusingopen('my_file.csv'),readalllinesintoavariableusingf.readlines().Now,iterateoveralllines,stripthemfromwhitespaceusingstrip(),andsplitthemonthedelimiter','usingsplit(','). Youcanaccomplishthisinasimplelistcomprehensionstatementlikeso: csv_filename='my_file.csv' withopen(csv_filename)asf: lines=f.readlines() lst=[line.strip().split(',')forlineinlines] print(lst) FeelfreetocheckoutmydetailedvideoincaseyouneedarefresheronthepowerfulPythonconceptlistcomprehension: ASimpleIntroductiontoListComprehensioninPython 🌍RelatedTutorial:UnderstandingListComprehensioninPython. MorePythonCSVConversions 🐍LearnMore:Ihavecompiledan“ultimateguide”ontheFinxterblogthatshowsyouthebestmethod,respectively,toconvertaCSVfiletoJSON,Excel,dictionary,Parquet,list,listoflists,listoftuples,textfile,DataFrame,XML,NumPyarray,andlistofdictionaries. BecomeaOne-LinerWizard! Incaseyouenjoyedtheone-linerspresentedhereandyouwanttoimproveyourPythonskills,feelfreetogetyourselfacopyofmybest-sellingPythonbook: PythonOne-LinersBook:MastertheSingleLineFirst! Pythonprogrammerswillimprovetheircomputerscienceskillswiththeseusefulone-liners. PythonOne-Linerswillteachyouhowtoreadandwrite“one-liners”:concisestatementsofusefulfunctionalitypackedintoasinglelineofcode.You’lllearnhowtosystematicallyunpackandunderstandanylineofPythoncode,andwriteeloquent,powerfullycompressedPythonlikeanexpert. Thebook’sfivechapterscover(1)tipsandtricks,(2)regularexpressions,(3)machinelearning,(4)coredatasciencetopics,and(5)usefulalgorithms. Detailedexplanationsofone-linersintroducekeycomputerscienceconceptsandboostyourcodingandanalyticalskills.You’lllearnaboutadvancedPythonfeaturessuchaslistcomprehension,slicing,lambdafunctions,regularexpressions,mapandreducefunctions,andsliceassignments. You’llalsolearnhowto: Leveragedatastructurestosolvereal-worldproblems,likeusingBooleanindexingtofindcitieswithabove-averagepollutionUseNumPybasicssuchasarray,shape,axis,type,broadcasting,advancedindexing,slicing,sorting,searching,aggregating,andstatisticsCalculatebasicstatisticsofmultidimensionaldataarraysandtheK-MeansalgorithmsforunsupervisedlearningCreatemoreadvancedregularexpressionsusinggroupingandnamedgroups,negativelookaheads,escapedcharacters,whitespaces,charactersets(andnegativecharacterssets),andgreedy/nongreedyoperatorsUnderstandawiderangeofcomputersciencetopics,includinganagrams,palindromes,supersets,permutations,factorials,primenumbers,Fibonaccinumbers,obfuscation,searching,andalgorithmicsorting Bytheendofthebook,you’llknowhowtowritePythonatitsmostrefined,andcreateconcise,beautifulpiecesof“Pythonart”inmerelyasingleline. GetyourPythonOne-LinersonAmazon!! ChrisWhileworkingasaresearcherindistributedsystems,Dr.ChristianMayerfoundhisloveforteachingcomputersciencestudents. TohelpstudentsreachhigherlevelsofPythonsuccess,hefoundedtheprogrammingeducationwebsiteFinxter.com.He’sauthorofthepopularprogrammingbookPythonOne-Liners(NoStarch2020),coauthoroftheCoffeeBreakPythonseriesofself-publishedbooks,computerscienceenthusiast,freelancer,andownerofoneofthetop10largestPythonblogsworldwide. Hispassionsarewriting,reading,andcoding.ButhisgreatestpassionistoserveaspiringcodersthroughFinxterandhelpthemtoboosttheirskills.Youcanjoinhisfreeemailacademyhere. RelatedTutorialsTheUltimateGuidetoPythonListsPythonListofLists-AHelpfulIllustratedGuidetoNested…56PythonOne-LinerstoImpressYourFriendsPythonOneLineXPythonJoinList[UltimateGuide]TheUltimateGuideonConvertingaCSVinPython WhyFinxter? "Givemealeverlongenough[...]andIshallmovetheworld."-ArchimedesFinxteraimstobeyourlever!Oursinglepurposeistoincreasehumanity'scollectiveintelligenceviaprogrammingtutorialssoyoucanleverageinfinitecomputationalintelligencetoyoursuccess!FinxterMissionVideo LearningResources Toboostyourskills,joinourfreeemailacademywith1000+tutorialsonPython,freelancing,datascience,machinelearning,andBlockchaindevelopment!Tocreateyourthrivingcodingbusinessonline,checkoutourFinxterbooksandtheworld's#1freelancedeveloperprogram.Ifyou'renotquitereadytogoall-in,watchthefreemasterclassonbuildingyourhigh-incomeskillprogramming. NewFinxterTutorials: SolidityDeepDive—Syllabus+VideoTutorialResources SolidityFunctionTypes—ASimpleGuidewithVideo User-DefinedValueTypesinSolidity SolidityStringTypes,Unicode/HexLiterals,andEnums HowtoRemoveTextWithinParenthesesinaPythonString? PythonPrintDictionaryValuesWithout“dict_values” HowtoCleanandFormatPhoneNumbersinPython PythonPrintDictionaryWithoutOneKeyorMultipleKeys StateVariablesinSolidity HowtoExtractaZipFileinPython FinxterCategories: Categories SelectCategory 2-minComputerScienceConcepts 2-minComputerSciencePapers AlexaSkills Algorithms AppDevelopment Arduino ArtificialIntelligence Automation BeautifulSoup Binary Bitcoin Blockchain Blogging Brownie C C# C++ Career CheatSheets Clojure CloudComputing CodingBusiness CodingInterview ComputerScience Crypto CSS CSV DailyDataSciencePuzzle DailyPythonPuzzle dApp Dash DataScience DataStructures DataVisualization Database DeepLearning DeFi DependencyManagement DevOps DistributedSystems Django DunderMethods Error Ethereum Excel ExceptionHandling Finance Flask Float Freelancing FunctionalProgramming Functions Git Go GraphTheory GUI Hardware HTML ImageProcessing Input/Output Investment Java JavaScript json Jupyter Keras Linux MachineLearning macOS Math Matplotlib NaturalLanguageProcessing Networking Newspaper3k NFT ObjectOrientation OpenCV OperatingSystem PandasLibrary Performance PHP Pillow pip Polygon Powershell Productivity Projects PyCharm PyTest Python PythonBuilt-inFunctions PythonDictionary PythonEmailCourse PythonKeywords PythonList PythonOne-Liners PythonOperators PythonRequests PythonSet PythonString Pythonsys PythonTime PythonTuple PyTorch React Regex Research Scikit-learnLibrary SciPy Scripting Seaborn Security Selenium shutil sklearn SmartContracts Solana Solidity SQL Statistics Streamlit SymPy Tableau TensorFlow Testing TextProcessing TheNumpyLibrary TKinter Trading VisualStudio Web3 WebDevelopment WebScraping Windows XML
延伸文章資訊
- 1Read a CSV into list of lists in Python - GeeksforGeeks
In this article, we are going to see how to read CSV files into a list of lists in Python. Method...
- 2Reading Rows from a CSV File in Python - GeeksforGeeks
- 3How to read a `csv` file into a list in Python - Adam Smith
Call open(filepath, mode) with a .csv file path as filepath and "r" as mode to return a file obje...
- 4How to read specific column from CSV file in Python - Adam Smith
- 5Python - Read CSV Columns Into List - GeeksforGeeks
In this method we will import the csv library and open the file in reading mode, then we will use...