DEV Community

Guru prasanna
Guru prasanna

Posted on

Python Day-19 csv file,String methods,ASCII,Task

CSV File:
-->Comma seperated value files.
-->It is a plain text format with series of values seperated by commas.
-->It stores all lines and fields in rows and columns
-->It can be opened with any text editor in windows.

Format:

f =open("sample.txt", "r")

with open("sample.txt",’r’) as f:
Enter fullscreen mode Exit fullscreen mode

r-read: Opens the file for reading
w-write: Opens the file for writing. Creates a new file or overwrites an existing one.
rb-read binary: This is used to read binary files like images, videos, audio files, PDFs, or any non-text files.

Example:
score.csv:

Player,Score
Virat,80
Rohit,90
Dhoni,100
Enter fullscreen mode Exit fullscreen mode

From another module:

import csv
f =open("score.csv", "r")
csv_reader = csv.reader(f)
for row in csv_reader:
    print(row)
f.close()
Enter fullscreen mode Exit fullscreen mode

Output:

['Player', 'Score']
['Virat', '80']
['Rohit', '90']
['Dhoni', '100']

Enter fullscreen mode Exit fullscreen mode

ASCII
American standard code for information interchange(ASCII)

Ascii Table:
Refer: https://www.w3schools.com/charsets/ref_html_ascii.asp

48-57 - Numbers
65-91 - A to Z
97-122- a to z

ord-ordinal-->To find ASCII number
chr-character-->To convert number to character

Pattern formation using ASCII:
1)

for row in range(5):
    for col in range(row+1):
        print(chr(col+65), end=' ')
    print()
Enter fullscreen mode Exit fullscreen mode

Output:

A 
A B 
A B C 
A B C D 
A B C D E 
Enter fullscreen mode Exit fullscreen mode

2)

for row in range(5):
    for col in range(5-row):
        print(chr(row+65), end=' ')
    print()
Enter fullscreen mode Exit fullscreen mode

Output:

A A A A A 
B B B B 
C C C 
D D 
E 
Enter fullscreen mode Exit fullscreen mode

Printing name using for loop and while loop:
Method-1:

name = 'guru'
for letter in name:
    print(letter,end=' ')
Enter fullscreen mode Exit fullscreen mode

Method-2:

name = 'guru'
i = 0
while i<len(name):
    print(name[i],end=' ')
    i+=1
Enter fullscreen mode Exit fullscreen mode

Output:

g u r u
Enter fullscreen mode Exit fullscreen mode

String methods using ASCII:
1. Capitalize: To convert first character into uppercase.

txt = "hello, and welcome to my world."

first = txt[0]
if first>='a' and first<='z':
    first = ord(first)-32
    first = chr(first)

print(f"{first}{txt[1:]}")
Enter fullscreen mode Exit fullscreen mode

Output:

Hello, and welcome to my world.
Enter fullscreen mode Exit fullscreen mode

2. casefold: To convert string into lowercase.

txt = "GUruprasanna!"

for letter in txt:
    if letter>='A' and letter<'Z':
        letter = ord(letter)+32
        letter = chr(letter)
    print(letter,end='')
Enter fullscreen mode Exit fullscreen mode

Output:

guruprasanna!
Enter fullscreen mode Exit fullscreen mode

3. Count: Returns the number of times a specified value occurs in a string.

txt = "I love apples, apple is my favorite fruit"
key = 'apple'
l = len(key)
count = 0
start = 0 
end = l
while end<len(txt):
    if txt[start:end] == key:
        count+=1
    start+=1
    end+=1
else:
    print(count)
Enter fullscreen mode Exit fullscreen mode

Output:

2
Enter fullscreen mode Exit fullscreen mode
#First Occurrence of given key
txt = "I love apples, apple is my favorite fruit"
key = 'apple'
l = len(key)
start = 0 
end = l
while end<len(txt):
    if txt[start:end] == key:
        print(start)
        break
    start+=1
    end+=1
Enter fullscreen mode Exit fullscreen mode

Output:

7
Enter fullscreen mode Exit fullscreen mode
#Last Occurrence of given key
txt = "I love apples, apple is my favorite fruit"
key = 'apple'
l = len(key)
start = 0 
end = l
final = 0
while end<len(txt):
    if txt[start:end] == key:
        final = start
    start+=1
    end+=1
else:
    print(final)
Enter fullscreen mode Exit fullscreen mode

Output:

15
Enter fullscreen mode Exit fullscreen mode

Task:
Find program for given output:

1   2   3   4   5   6   7   
1   2   3   4   5
1   2   3
1
Enter fullscreen mode Exit fullscreen mode

Input:

for row in range(4):
    for col in range(7-(row*2)):
        print((col+1), end=' ')
    print()
Enter fullscreen mode Exit fullscreen mode

Top comments (0)