27 Set5 PDS

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

8/3/2021 Practical Set 5 - Jupyter Notebook

Practical set :- 5

Implemented By :- 190760107027

27. Python Program demonstrate following : a) Read


content of text file using uploading, streaming and
sampling methods from “Colors.txt” file b) Read Content
of CSV file c) Read data from Excel sheet

a) Read content of text file using uploading, streaming and sampling


methods from “Colors.txt” file

In [16]: 1 with open("colors.txt") as color:


2 for data in color:
3 print(data)

Color Value

Blue 1

Black 2

White 3

Green 4

Red 5

Yellow 6

Magenta 7

Cyan 8

Purple 9

Pink 10

localhost:8888/notebooks/PDS Practical/Practical Set 5.ipynb 1/6


8/3/2021 Practical Set 5 - Jupyter Notebook

In [4]: 1 with open("colors.txt") as color:


2 print(color.read())

Color Value

Blue 1

Black 2

White 3

Green 4

Red 5

Yellow 6

Magenta 7

Cyan 8

Purple 9

Pink 10

In [13]: 1 num = 3
2 with open("colors.txt") as color:
3 for index, data in enumerate(color):
4 if index % num == 0:
5 print(f"Index : {index}, line: {data}")

Index : 0, line: Color Value

Index : 3, line: White 3

Index : 6, line: Yellow 6

Index : 9, line: Purple 9

localhost:8888/notebooks/PDS Practical/Practical Set 5.ipynb 2/6


8/3/2021 Practical Set 5 - Jupyter Notebook

In [14]: 1 import random


2 ​
3 with open("colors.txt") as color:
4 for index, data in enumerate(color):
5 num = random.random()
6 print(num)
7 if num <= 0.30:
8 print(f"Index : {index}, line: {data}")

0.8273041205294981

0.4832116347831039

0.10678505093022406

Index : 2, line: Black 2

0.36176305196078795

0.3150499470107109

0.35566184656440303

0.11726326020016775

Index : 6, line: Yellow 6

0.25132981078751415

Index : 7, line: Magenta 7

0.056989782261438804

Index : 8, line: Cyan 8

0.5699636014819119

0.5567600679035656

b) Read Content of CSV file

In [12]: 1 import pandas as pd

In [14]: 1 csv_data = pd.io.parsers.read_csv("result.csv")


2 csv_data

Out[14]: Name Physics Chemistry Maths

0 Dosan 98 85 95

1 Paxton 75 80 86

2 Daalmi 92 97 92

c) Read data from Excel sheet

localhost:8888/notebooks/PDS Practical/Practical Set 5.ipynb 3/6


8/3/2021 Practical Set 5 - Jupyter Notebook

In [15]: 1 excel_data = pd.read_excel("result.xlsx")


2 excel_data

Out[15]: FirstName LastName Contact

0 Ariana Grande 5428751214

1 Devi Vishvakumar 9168632863

2 Paxton Yoshida 5458458157

28. Write a python program to plot image and display it’s


information. Generate another image from previous image
by cropping it
In [44]: 1 from skimage.io import imread
2 from matplotlib import pyplot as plt

In [46]: 1 image_file = "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQi_FnpZC


2 image = imread(image_file)
3 plt.imshow(image)
4 plt.show()

In [50]: 1 print(f"Data type :- {type(image)}\nSize of image :- {image.shape}")

Data type :- <class 'numpy.ndarray'>

Size of image :- (225, 225, 3)

localhost:8888/notebooks/PDS Practical/Practical Set 5.ipynb 4/6


8/3/2021 Practical Set 5 - Jupyter Notebook

In [48]: 1 image2 = image[10:175, 20:170]


2 plt.imshow(image2)
3 plt.show()

29. Write a python program to perform XML Parsing


In [17]: 1 import pandas as pd
2 from lxml.objectify import *

In [19]: 1 xml = parse(open("student.xml"))


2 root = xml.getroot()
3 print(root.tag)

student

In [20]: 1 data = pd.DataFrame(columns=("Firstname", "Surname", "Contact"))


2 for i in range(3):
3 obj = root.getchildren()[i].getchildren()
4 dict = {
5 "Firstname": obj[0].text,
6 "Surname": obj[1].text,
7 "Contact": obj[2].text
8 }
9 row = pd.Series(dict)
10 row.name = i
11 data = data.append(row)

localhost:8888/notebooks/PDS Practical/Practical Set 5.ipynb 5/6


8/3/2021 Practical Set 5 - Jupyter Notebook

In [21]: 1 data

Out[21]: Firstname Surname Contact

0 Sunny Kadia 1356862863

1 Mitva Jariwala 91873386263

2 Dosan Naam 42956862863

localhost:8888/notebooks/PDS Practical/Practical Set 5.ipynb 6/6

You might also like