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

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

Susan Sarandon
Release: 2024-12-14 08:05:12
Original
746 people have browsed it

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

CSV File:
-->Comma seperated 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:
Copy after login

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

From another module:

import csv
f =open("score.csv", "r")
csv_reader = csv.reader(f)
for row in csv_reader:
    print(row)
f.close()
Copy after login

Output:

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

Copy after login

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

Output:

A 
A B 
A B C 
A B C D 
A B C D E 
Copy after login

2)

for row in range(5):
    for col in range(5-row):
        print(chr(row+65), end=' ')
    print()
Copy after login

Output:

A A A A A 
B B B B 
C C C 
D D 
E 
Copy after login

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

name = 'guru'
for letter in name:
    print(letter,end=' ')
Copy after login

Method-2:

name = 'guru'
i = 0
while i<len(name):
    print(name[i],end=' ')
    i+=1
Copy after login

Output:

g u r u
Copy after login

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:]}")
Copy after login

Output:

Hello, and welcome to my world.
Copy after login

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

Output:

guruprasanna!
Copy after login

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

Output:

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

Output:

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

Output:

15
Copy after login

Task:
Find program for given output:

1   2   3   4   5   6   7   
1   2   3   4   5
1   2   3
1
Copy after login

Input:

for row in range(4):
    for col in range(7-(row*2)):
        print((col+1), end=' ')
    print()
Copy after login

The above is the detailed content of Python Day-csv file,String methods,ASCII,Task. 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