DEV Community

Lakshmi Pritha Nadesan
Lakshmi Pritha Nadesan

Posted on

Day 19 - CSV file, ASCII, String methods

CSV (Comma Separated Values):

CSV file represents a row, and each value within the row is separated by a comma.
CSV file look like Excel but Excel file open only in excel software.
CSV file is used all the operating system.

We can open the CSV file in the following two formats.


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. File must exist.
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.

store.csv

Player,Score
Virat,80
Rohit,90
Dhoni,100
Enter fullscreen mode Exit fullscreen mode
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
['Player', 'Score']
['Virat', '80']
['Rohit', '90']
['Dhoni', '100']
Enter fullscreen mode Exit fullscreen mode

ASCII:
ASCII stands for American Standard Code for Information Interchange.

ASCII table:
48-57 - Numbers(Digits 0 to 9)
65-90 - A-Z(Uppercase letters)
97-122 - a-z(Lowercase letters)

Pattern Programs Using ASCII table:

for row in range(5):
    for col in range(row+1):
        print(chr(col+65), end=' ')
    print()
Enter fullscreen mode Exit fullscreen mode
A 
A B 
A B C 
A B C D 
A B C D E 
Enter fullscreen mode Exit fullscreen mode
for row in range(5):
    for col in range(5-row):
        print(chr(row+65), end=' ')
    print()
Enter fullscreen mode Exit fullscreen mode
A A A A A 
B B B B 
C C C 
D D 
E 
Enter fullscreen mode Exit fullscreen mode

Using for loop:

name = 'pritha'
for letter in name:
    print(letter,end=' ')

Enter fullscreen mode Exit fullscreen mode
P r i t h a
Enter fullscreen mode Exit fullscreen mode

Using while loop:

name = 'pritha'
i=0
while i<len(name):
    print(name[i],end=' ')
    i+=1
Enter fullscreen mode Exit fullscreen mode
P r i t h a
Enter fullscreen mode Exit fullscreen mode

string methods:
1. capitalize()
The capitalize() method in Python is used to convert the first character of a string to uppercase and make all other characters lowercase.

txt = "hello, and welcome to my world."
x = txt.capitalize()
print (x)

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

Write a capitalize program using ASCII table:

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

first = txt[0]
first = ord(first)-32
first = chr(first)

print(f'{first}{txt[1:]}')
Enter fullscreen mode Exit fullscreen mode
Hello, and welcome to my world.
Enter fullscreen mode Exit fullscreen mode

2.casefold()
The casefold() method in Python is used to convert a string to lowercase.

txt = "Hello, And Welcome To My World!"
x = txt.casefold()
print(x)
Enter fullscreen mode Exit fullscreen mode
hello, and welcome to my world!
Enter fullscreen mode Exit fullscreen mode

Write a casefold program using ASCII table:

txt = "Hello, And Welcome To My World!"
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
hello, and welcome to my world!
Enter fullscreen mode Exit fullscreen mode

3.count()
The count() method in Python is used to count the occurrences of a substring within a string.

txt = "I love apples, apple is my favorite fruit"
x = txt.count("apple")
print(x)

Enter fullscreen mode Exit fullscreen mode
2
Enter fullscreen mode Exit fullscreen mode

Write a count program for given key:

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
2
Enter fullscreen mode Exit fullscreen mode

Write a program to 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

7
Enter fullscreen mode Exit fullscreen mode

Write a program to 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
15
Enter fullscreen mode Exit fullscreen mode

Task:

for row in range(4):
    for col in range(7-(row*2)):
        print((col+1),end=" ") 
    print()

Enter fullscreen mode Exit fullscreen mode
1 2 3 4 5 6 7 
1 2 3 4 5 
1 2 3 
1 
Enter fullscreen mode Exit fullscreen mode
for row in range(5):
    for col in range(5-row):
        print((row+1)+(col*2),end=" ") 
    print()
Enter fullscreen mode Exit fullscreen mode
1 3 5 7 9 
2 4 6 8 
3 5 7 
4 6 
5 
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
payilagam_135383b867ea296 profile image
Payilagam

Good Consistency! Keep it up!