Home > Backend Development > Python Tutorial > Day - CSV file, ASCII, String methods

Day - CSV file, ASCII, String methods

Patricia Arquette
Release: 2024-12-21 07:17:11
Original
390 people have browsed it

Day - 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:

Copy after login

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
Copy after login
import csv
f =open("score.csv", "r")
csv_reader = csv.reader(f)
for row in csv_reader:
    print(row)
f.close()

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

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()
Copy after login
A 
A B 
A B C 
A B C D 
A B C D E 
Copy after login
for row in range(5):
    for col in range(5-row):
        print(chr(row+65), end=' ')
    print()
Copy after login
A A A A A 
B B B B 
C C C 
D D 
E 
Copy after login

Using for loop:

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

Copy after login
P r i t h a
Copy after login
Copy after login

Using while loop:

name = 'pritha'
i=0
while i<len(name):
    print(name[i],end=' ')
    i+=1
Copy after login
P r i t h a
Copy after login
Copy after login

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)

Copy after login
Hello, and welcome to my world.
Copy after login
Copy after login

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:]}')
Copy after login
Hello, and welcome to my world.
Copy after login
Copy after login

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)
Copy after login
hello, and welcome to my world!
Copy after login
Copy after login

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='')

Copy after login
hello, and welcome to my world!
Copy after login
Copy after login

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)

Copy after login
2
Copy after login
Copy after login

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)
Copy after login
2
Copy after login
Copy after login

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
Copy after login
7
Copy after login

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)
Copy after login
15
Copy after login

Task:

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

Copy after login
1 2 3 4 5 6 7 
1 2 3 4 5 
1 2 3 
1 
Copy after login
for row in range(5):
    for col in range(5-row):
        print((row+1)+(col*2),end=" ") 
    print()
Copy after login
1 3 5 7 9 
2 4 6 8 
3 5 7 
4 6 
5 
Copy after login

The above is the detailed content of Day - CSV file, ASCII, String methods. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template