Introduction to Programming with Python

Slides:



Advertisements
Similar presentations
Computer Science 111 Fundamentals of Programming I Files.
Advertisements

Last Week Looping though lists Looping using range While loops.
Files CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
CIT 590 Intro to Programming Files etc. Announcements From HW5 onwards (HW5, HW6,…) You can work alone. You can pick your own partner. You can also stick.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 18 Using text files to share data with other programs 5/07/09 Python Mini-Course:
File I/O Ruth Anderson UW CSE 160 Spring File Input and Output As a programmer, when would one use a file? As a programmer, what does one do with.
Files Tutor: You will need ….
Last Week Lists List methods Nested Lists Looping through lists using for loops.
1 Introduction to Python LING 5200 Computational Corpus Linguistics Martha Palmer.
FILES. open() The open() function takes a filename and path as input and returns a file object. file object = open(file_name [, access_mode][, buffering])
CIT 590 Intro to Programming Files etc. Agenda Files Try catch except A module to read html off a remote website (only works sometimes)
CSV Files Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Python focus – files The open keyword returns a file object Opening a file myFile = open('C:\file.txt', arg) Optional argument The second argument controls.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 10: Files.
File Processing Upsorn Praphamontripong CS 1110
CMSC201 Computer Science I for Majors Lecture 10 – File I/O
Topic: File Input/Output (I/O)
For loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
File Writing Upsorn Praphamontripong CS 1110
Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Python’s input and output
CSC 108H: Introduction to Computer Programming
Ruth Anderson UW CSE 160 Winter 2016
File input and output Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble Notes from 2009: Sample problem.
පාඨමාලා මාතෘකා Microsoft SQL Server Databases සැකසීම
Python I/O.
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
Introduction to Programming with Python
Microsoft Azure Fundamentals Microsoft Azure මූලික දැනුම
Introduction to Programming with Python
Introduction to Programming with Python
Introduction to Programming with Python
Introduction to Programming with Python
File IO and Strings CIS 40 – Introduction to Programming in Python
Fundamentals of Programming I Files
File Handling.
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
Customizing Views Views Customize කර ගැනීම
ETI Base Code ETI මුලධර්මයන් හදුනාගනිමු
Microsoft Azure Fundamentals Microsoft Azure මූලික දැනුම
මුහම්මද් සල්ලල්ලාහු අලෙයිහි වසල්ලම්
Basics of MVC MVC වල මුලික කොටස්
Fundamentals of Data Structures
Microsoft Azure Fundamentals Microsoft Azure මූලික දැනුම
Creating and Configuring Models Models create කිරීම සහ config කර ගැනීම
Lists in Python Creating lists.
වී පොහොර සහනාධාර මුදල් ප්‍රදාන වැඩසටහන 2017/18 මහ කන්නය
CS 1111 Introduction to Programming Fall 2018
Customizing Controllers Controllers Customizing කර ගැනීම
How to save information in files open, write, close
‘Shifters’ in bilingual education : Problems and challenges of learning through a second language Marie Perera 1Department of Humanities Education, University.
Introduction to Programming with Python
Introduction to Programming with Python
Introduction to Programming with Python
Topics Introduction to File Input and Output
Microsoft Azure Fundamentals Microsoft Azure මූලික දැනුම
Introduction to Programming with Python
Introduction to Programming with Python
Introduction to Programming with Python
Python 16 Mr. Husch.
Introduction to Computer Science
Python 16 Mr. Husch.
Topics Introduction to File Input and Output
CS 1111 Introduction to Programming Spring 2019
How to read from a file read, readline, reader
Introduction to Computer Science
Presentation transcript:

Introduction to Programming with Python File එකක තොරතුරු කියවීම Dileepa S. Rajapaksa | http://www.windowsgeek.lk @dsrajapaksa Microsoft Virtual Academy

කලින් Video එකෙන් … File සමග වැඩ කිරීම. තොරතුරු file එකකට ලිවීම open() Function එක භාවිතය Write function එක භාවිතය CSV Files භාවිතය. Microsoft Virtual Academy

Demo කලින් Video එකේ අභියෝගයට පිළිතුරු Microsoft Virtual Academy

File එකක් කියවීම read, readline, reader Microsoft Virtual Academy Last part Practice your language Microsoft Virtual Academy

අපි ලියාගත් ගොනුවක් ප්‍රයෝජනවත් වන්නේ එය නැවත කියවීමට හැකිනම් පමණයි. කඩයට ගිහින් බඩු ලැයිස්තුව කියවීමෙන් මිලදීගැනීමට අවශ්‍ය බඩු දැනගන්න පුළුවන්. අමුත්තන්ගේ නම් ලැයිස්තුවේ ඇති නම් ප්‍රමාණය දැනගැනීමෙන් ඔබට පුළුවන් ප්‍රමාණවත් තරම් ආහාර තියනවාදැයි දැනගන්න. දුරකතන අංක බැලීමෙන් පසු ඒ අංක වලට ඇමතුම් ලබාගන්න පුළුවන්. Microsoft Virtual Academy

අපට වැඩසටහන් වලදී නිතරම file වල save කරගත් තොරතුරු කියවන්න සිදුවෙනවා. ඔබේ e-book කියවනය ආරම්භයේදී, එය විසින් ඔබ අන්තිමට බලමින් සිටි පිටුව කුමක්දැයි බලනවා. ඔබ ක්‍රීඩාකරමින් සිටි පරිගණක ක්‍රීඩාව නැවත අරඹන විට පෙර ලකුණු හා ඔබ සම්පූර්ණ කල මෙහෙයුම් පරීක්ෂාකරනවා අන්තර්ජාලයේ පරික්ෂා කර බැලුවොත් වටිනා දත්ත ඇති විවෘත ගොනු රාශියක් ඔබට හොයාaගන්න පුලුවන් Microsoft Virtual Academy

Text files Last part Practice your language Microsoft Virtual Academy

code එක මගින් file කියවන්නේ කොහොමද? open function එක භාවිතය. myFile = open(fileName, accessMode) ටිකක් පුරුදුයි වගේද? ඔව් ඒ අපි file එකක ලිවීමට භාවිතා කළ function එකමයි. Program එක කොහොමද දැනගන්නේ read කිරීමටද write කිරීමටද බව? access mode එක Access mode Action r Read the file w Write to the file a Append to the existing file content b Open a binary file Microsoft Virtual Academy

File එකක අන්තර්ගතය කියවන්නේ කොහොමද? Read method එක භාවිතය. fileContent= myFile.read() Read method එක මගින් file එකේ ඇති මුළු අන්තර්ගතයම string variable එකකට ලබාදෙනවා. Microsoft Virtual Academy

ඔබට කැමතිනම් එක පේළිය බැගින් කියවන්නත් පුළුවන්. readline method එක භාවිතය fileContent= myFile.readline() readline method එක මගින් ලබාදෙන්නේ file එකේ එක පේළියක් පමණයි. Microsoft Virtual Academy

Demo File එකක් කියවීම Microsoft Virtual Academy Last part Practice your language Microsoft Virtual Academy

CSV File Last part Practice your language Microsoft Virtual Academy

ඔබ CSV File කියවනවා නම්, CSV library එක ඔබට උදව් කරාවි. CSV library එකේ ඇති අංග භාවිතයට CSV library එක import කළ යුතුයි. import csv Microsoft Virtual Academy

දැන් ඔබට reader function එක භාවිතයෙන් file එකකින් සියළුම පේළි list එකකට ලබාගන්න පුළුවන්. reader function එක මගින් file එකේ තියන සෑම පේළියම list එකකට ඇතුලත් කරගන්නවා. dataFromFile = csv.reader(myCSVfile) ඔබේ csv file එකේ data වෙන්කිරීමට භාවිතා කර ඇත්තේ කොමාව නොවේනම් , ඒ වෙනුවට භාවිතා කළ character එක ලබාදෙන්න. dataFromFile = csv.reader(myCSVFile, delimiter=",") Microsoft Virtual Academy

දැන් අපිට පුළුවන් csv file විවෘත කරන්න සහ කියවන්න fileName = "GuestList.txt“ accessMode = "r" with open(fileName, accessMode) as myCSVFile:      #Read the file contents dataFromFile = csv.reader(myCSVFile) Microsoft Virtual Academy

“with “සහ “:” ඇයි. with open(fileName, accessMode) as myCSVFile: Program එක විසින් හැම විටම file එකක් open කලයුතුයි. ඉන් අනතුරුව එය close කලයුතුයි. එහෙම නොකලොත්.සමහර විට open කරපු file එකක් නැවත open කිරීමට යාමේදී ඔබේ code එක crash වෙන්න පුළුවන්. With හා : syntax එක භාවිතයෙන් ඔබගේ Code එක Crash වුවහොත් හෝ සාර්ථකව run වුවහොත් file එක close කරනු ලබනවා Microsoft Virtual Academy

CSV file එකෙන් සියළුම පේළි ලබා ගත් පසුව එක් පේළියක් පමණක් භාවිතා කරන්නේ කොහොමද? ලැයිස්තුව දිගේ ගමන් කිරීමට for loop එක භාවිතා කරන්න සෑම පෙලියකම එක් දත්ත ලැයිස්තුවක් අඩංගුවෙනවා with open(fileName, accessMode) as myCSVFile: #Read the file contents dataFromFile = csv.reader(myCSVFile) #For loop that will run once per row for row in dataFromFile :         print(row) Microsoft Virtual Academy

මේ සියළුම දේවල් සමග code එක මේ ආකාර වේවි. fileName = "GuestList.txt" accessMode = "r" with open(fileName, accessMode) as myCSVFile: #Read the file contents dataFromFile = csv.reader(myCSVFile) #For loop that will run #once per row for row in dataFromFile :         print(row) Microsoft Virtual Academy

Demo CSV file එකක් කියවීම Microsoft Virtual Academy Last part Practice your language Microsoft Virtual Academy

Nested Loop මගින් පේළියක ඇති වචන ද ලබාගත හැකියි. මට පේළියේ ඇති සම්පූර්ණ අගයම print කිරීම හැර. තනි අගයන් ලබාගැනීමට අවශ්‍යනම්. ලැබෙන පෙලි වල අන්තර්ගතය වන්නේ ලැයිස්තූන් වේ for row in dataFromFile :        print(row) for value in row :             print(value + "\n") Nested Loop මගින් පේළියක ඇති වචන ද ලබාගත හැකියි. Microsoft Virtual Academy

SeparatorToDisplay.join(myList) for row in dataFromFile : ලැයිස්තුව Print වෙනවිට ලැබෙන වරහන් හා පෙරලි කොමා ඉවත් කරගන්න අවශ්‍ය වුනොත්? Format එක සැකසීම සඳහා join function එක ඔබට භාවිතා කරන්න පුළුවන්. SeparatorToDisplay.join(myList) for row in dataFromFile :           print (', '.join(row)) Microsoft Virtual Academy

Demo CSV file එකෙන් තනි අගයන් කියවා ගැනීම Microsoft Virtual Academy Last part Practice your language Microsoft Virtual Academy

අභියෝගය… අමුත්තගේ ලැයිස්තුව ඇතුලත් CSV file එකෙන් අමුත්තන්ගේ නම් සහ වයස් ගෙන print කිරීමට program එකක් ලියන්න. Microsoft Virtual Academy

සාරාංශය… File එකක් කියවීම CSV file එකක් කියවීම with හා : syntax එක භාවිතාකිරීම CSV file එකෙන් තනි අගයන් කියවා ගැනීම Microsoft Virtual Academy

සුභ පැතුම් ! ඔබට දැන් ගොනුවක තැන්පත්කල දත්ත කියවීමට හැකි වැඩසටහනක් නිර්මාණය කල හැකිය… Microsoft Virtual Academy