Python: Read specific columns of a given CSV file and print ...

文章推薦指數: 80 %
投票人數:10人

Python Exercises, Practice and Solution: Write a Python program to read specific columns of a given CSV file and print the content of the ... home FrontEnd HTML CSS JavaScript HTML5 Schema.org php.js TwitterBootstrap ResponsiveWebDesigntutorial ZurbFoundation3tutorials PureCSS HTML5Canvas JavaScriptCourse Icon Angular Vue Jest Mocha NPM Yarn BackEnd PHP Python Java Node.js Ruby Cprogramming PHPComposer Laravel PHPUnit Database SQL(2003standardofANSI) MySQL PostgreSQL SQLite NoSQL MongoDB Oracle Redis ApolloGraphQL API GooglePlusAPI YoutubeAPI GoogleMapsAPI FlickrAPI Last.fmAPI TwitterRESTAPI DataInterchnage XML JSON Ajax Exercises HTMLCSSExercises JavaScriptExercises jQueryExercises jQuery-UIExercises CoffeeScriptExercises PHPExercises PythonExercises CProgrammingExercises C#SharpExercises JavaExercises SQLExercises OracleExercises MySQLExercises SQLiteExercises PostgreSQLExercises MongoDBExercises TwitterBootstrapExamples Others ExcelTutorials Usefultools GoogleDocsFormsTemplates GoogleDocsSlidePresentations NumberConversions LinuxTutorials Quizzes Articles Python:ReadspecificcolumnsofagivenCSVfileandprintthecontentofthecolumns LastupdateonAugust19202221:51:51(UTC/GMT+8hours) PythonCSVFileReadingandWriting:Exercise-7withSolution WriteaPythonprogramtoreadspecificcolumnsofagivenCSVfileandprintthecontentofthecolumns. SampleSolution: PythonCode: importcsv withopen('departments.csv',newline='')ascsvfile: data=csv.DictReader(csvfile) print("IDDepartmentName") print("---------------------------------") forrowindata: print(row['department_id'],row['department_name']) departments.csv department_id,department_name,manager_id,location_id 10,Administration,200,1700 20,Marketing,201,1800 30,Purchasing,114,1700 40,HumanResources,203,2400 50,Shipping,121,1500 60,IT,103,1400 70,PublicRelations,204,2700 80,Sales,145,2500 90,Executive,100,1700 100,Finance,108,1700 110,Accounting,205,1700 120,Treasury,,1700 130,CorporateTax,,1700 140,ControlAndCredit,,1700 150,ShareholderServices,,1700 160,Benefits,,1700 170,Manufacturing,,1700 180,Construction,,1700 190,Contracting,,1700 200,Operations,,1700 210,ITSupport,,1700 220,NOC,,1700 230,ITHelpdesk,,1700 240,GovernmentSales,,1700 250,RetailSales,,1700 260,Recruiting,,1700 270,Payroll,,1700 SampleOutput: IDDepartmentName --------------------------------- 10Administration 20Marketing 30Purchasing 40HumanResources 50Shipping 60IT 70PublicRelations 80Sales 90Executive 100Finance 110Accounting 120Treasury 130CorporateTax 140ControlAndCredit 150ShareholderServices 160Benefits 170Manufacturing 180Construction 190Contracting 200Operations 210ITSupport 220NOC 230ITHelpdesk 240GovernmentSales 250RetailSales 260Recruiting 270Payroll PythonCodeEditor: Haveanotherwaytosolvethissolution?Contributeyourcode(andcomments)throughDisqus. Previous:WriteaPythonprogramthatreadsaCSVfileandremoveinitialspaces,quotesaroundeachentryandthedelimiter. Next:WriteaPythonprogramthatreadseachrowofagivencsvfileandskiptheheaderofthefile.Alsoprintthenumberofrowsandthefieldnames. Whatisthedifficultylevelofthisexercise? Easy Medium Hard TestyourProgrammingskillswithw3resource'squiz.  SharethisTutorial/Exerciseon:Facebook andTwitter Python:TipsoftheDay Findingtheindexofaniteminalist: >>>x=["foo","bar","baz"] >>>x.index("bar") 1 Loading...   Exercises:WeeklyTop16MostPopularTopics SQLExercises,Practice,Solution-JOINS SQLExercises,Practice,Solution-SUBQUERIES JavaScriptbasic-Exercises,Practice,Solution JavaArray:Exercises,Practice,Solution CProgrammingExercises,Practice,Solution:ConditionalStatement HRDatabase-SORTFILTER:Exercises,Practice,Solution CProgrammingExercises,Practice,Solution:String PythonDataTypes:Dictionary-Exercises,Practice,Solution PythonProgrammingPuzzles-Exercises,Practice,Solution C++Array:Exercises,Practice,Solution JavaScriptconditionalstatementsandloops-Exercises,Practice,Solution C#SharpBasicAlgorithm:Exercises,Practice,Solution PythonLambda-Exercises,Practice,Solution PythonPandasDataFrame:Exercises,Practice,Solution ConversionTools JavaScript:HTMLFormValidation



請為這篇文章評分?