Python Variables and Operators - Python and CSV Files
Key features of Python:
· It is a general purpose programming language which can be, used for both scientific and non-scientific programming.
· It is a platform independent programming language.
· The programs written in Python are easily readable and understandable.
Python Coding:
· In Python, programs can be written in two ways namely Interactive mode and Script mode.
· The Interactive mode allows us to write codes in Python command prompt ( > > >) whereas in script mode programs can be written and stored as separate file with the extension . py and executed.
· Script mode is used to create and edit python source file.
Tokens.
Python breaks each logical line into a sequence of elementary lexical components known as Tokens.
The normal token types are
· Identifiers
· Keywords
· Operators
· Delimiters
· Literals
Operators used in Python :
· Arithmetic operators
· Relational or Comparative operator
· Logical operators
· Assignment operators
· Conditional operator
Input-output functions:
· The input () function helps to enter data at run time by the user .
· The output function print () is used to display the result of the program on the screen after execution.
Comment statement.:
# are considered as comments and ignored by the Python interpreter.
Python and CSV Files
SV file:
A CSV file is a human readable text file where each line has a number of fields, separated by commas or some other delimiter
Two ways to read a CSV file:
The two ways to read a CSV file are using csv.reader() function and using DictReader class.
Python File Modes :
Mode |
Description |
‘r’ |
Open a file for reading. (default) |
‘w’ |
Open a file for writing. Creates a new file if it does not exist or truncates the file if it exists. |
‘x’ |
Open a file for exclusive creation. If the file already exists, the operation fails. |
‘a’ |
Open for appending at the end of the file without truncating it. Creates a new file if it does not exist. |
‘t’ |
Open in text mode. (default) |
‘b’ |
Open in binary mode. |
‘+’ |
Open a file for updating (reading and writing) |
The default mode of csv file in reading and writing is text mode
Binary mode can be used when dealing with non-text files like image or exe files.
Garbage collector:
Python has a garbage collector to clean up unreferenced objects
Skip initial space:
“skip initial space” is used for removing whitespaces after the delimiter
CSV library:
The CSV library contains objects and other code to read, write, and process data from and to CSV files.
csv.reader , csv.writer,csv.DictReader ,csv.DictWriter :
· csv.reader and csv.writer work with list/tuple.
· csv.DictReader and csv.DictWriter work with dictionary .
· csv.DictReader and csv.DictWriter take additional argument fieldnames that are used as dictionary keys.
· The csv.writer() method returns a writer object which converts the user’s data into delimited strings.
· DictReader() class of csv module creates an object which maps data to a dictionary.
operator.itemgetter():
operator.itemgetter() can be used to sort by more than one column operator.itemgetter()
itemgetter():
itemgetter() with multiple indices is used to sort by more than one column.
writerow() Vs Writerows() :
The writerow() method writes one row at a time. Writerows() method is used to write all the data at once.
csv.register_dialect():
CSV file having custom delimiter is read with the help of csv.register_dialect().
close() method:
close() method will free up the resources that were tied with the file
Line Terminator:
A Line Terminator is a string used to terminate lines produced by writer.
Dialect :
A dialect is a class of csv module which helps to define parameters for reading and writing CSV.
Ordered Dict:
An Ordered Dict is a dictionary subclass which saves the order in which its contents are added.
The article is so informative. This is more helpful. Thanks for sharing
ReplyDeletePython training in chennai
Aimore Technologies is the best Python Training institute in Chennai with 10+ years of experience. We are offering online and classroom training. Visit Us: Python Training in Chennai . Python is an undeniable level, universally useful programming language. Its plan reasoning underscores code meaningfulness with the utilization of critical space. Its language development and object-arranged approach mean to assist software engineers with composing clear, legitimate code for little and enormous scope projects.
ReplyDelete