This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## DictReader | |
with open('student_details2.csv' , 'r') as csv_file2: | |
## Reading csv file | |
## Here without delimiter specified | |
file2 = csv.DictReader(csv_file2, delimiter="t") | |
## Output is different as it uses t as raw form. | |
for line in file2: | |
print(line) | |
#[Output]: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Name | Roll no. | Class | |
---|---|---|---|
Akshay | 1 | 2 | |
Rajesh | 24 | 3 | |
Bram | 12 | 6 | |
Srishika | 45 | 7 | |
Radhika | 78 | 6 | |
Bluenix | 67 | 6 |
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Name Roll no. Class | |
Akshay 1 2 | |
Rajesh 24 3 | |
Bram 12 6 | |
Srishika 45 7 | |
Radhika 78 6 | |
Bluenix 67 6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## It is simply generating error with tracebacks | |
## Dividing by zero is undefined. | |
print(10/0) | |
## Generating a traceback | |
#[Output]: | |
#--------------------------------------------------------------------------- | |
#ZeroDivisionError Traceback (most recent call last) | |
#<ipython-input-1-19547e02be0f> in <module>() | |
# 2 ## Dividing by zero is undefined. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Using try and except block trying the above code of diving by zero. | |
try: a = 10/0 | |
## It is simply generating an error in one line. | |
except Exception as e: | |
print(str(e)) | |
#[Output]: | |
#division by zero |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Setting the variable to integer value in order to generate exception. | |
a= 7 | |
if str(type(a)) == "<class 'int'>": | |
raise Exception('This variable cannot be integer') | |
#[Output]: | |
#-------------------------------------------------------------------------Exception: This variable cannot be integer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Creating a dictionary | |
dict1 = {'one': 1, 'two': 2, 'three': 3} | |
print(dict1) | |
#[Output]: | |
#{'three': 3, 'one': 1, 'two': 2} | |
## How to access each element in dictionary | |
print(dict1['one']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## If key doesn't exist. | |
## It will generate a keyerror | |
print(dict1['four']) | |
#[Output]: | |
#--------------------------------------------------------------------------- | |
#KeyError Traceback (most recent call last) | |
# in () | |
# 1 ## It will generate a keyerror | |
# 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## del() method | |
del dict1['one'] | |
print(dict1) | |
#[Output]: | |
#{'three': 3, 'two': 2} | |
## pop() method | |
dict1.pop('two') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## How to declare ordered dictionary | |
from collections import OrderedDict | |
dict12 = OrderedDict() | |
dict12['one'] = 1 | |
dict12['two'] = 2 | |
dict12['three'] = 3 | |
## It will print in a order of insertion. | |
print(dict12) | |
#[Output]: |