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

Day - CSV file, ASCII, String methods

Dec 21, 2024 am 07:17 AM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

How to solve permission issues when using python --version command in Linux terminal? How to solve permission issues when using python --version command in Linux terminal? Apr 02, 2025 am 06:36 AM

Using python in Linux terminal...

How to teach computer novice programming basics in project and problem-driven methods within 10 hours? How to teach computer novice programming basics in project and problem-driven methods within 10 hours? Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to get news data bypassing Investing.com's anti-crawler mechanism? How to get news data bypassing Investing.com's anti-crawler mechanism? Apr 02, 2025 am 07:03 AM

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...

Python 3.6 loading pickle file error ModuleNotFoundError: What should I do if I load pickle file '__builtin__'? Python 3.6 loading pickle file error ModuleNotFoundError: What should I do if I load pickle file '__builtin__'? Apr 02, 2025 am 06:27 AM

Loading pickle file in Python 3.6 environment error: ModuleNotFoundError:Nomodulenamed...

What is the reason why pipeline files cannot be written when using Scapy crawler? What is the reason why pipeline files cannot be written when using Scapy crawler? Apr 02, 2025 am 06:45 AM

Discussion on the reasons why pipeline files cannot be written when using Scapy crawlers When learning and using Scapy crawlers for persistent data storage, you may encounter pipeline files...

See all articles