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:
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
import csv f =open("score.csv", "r") csv_reader = csv.reader(f) for row in csv_reader: print(row) f.close()
['Player', 'Score'] ['Virat', '80'] ['Rohit', '90'] ['Dhoni', '100']
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()
A A B A B C A B C D A B C D E
for row in range(5): for col in range(5-row): print(chr(row+65), end=' ') print()
A A A A A B B B B C C C D D E
Using for loop:
name = 'pritha' for letter in name: print(letter,end=' ')
P r i t h a
Using while loop:
name = 'pritha' i=0 while i<len(name): print(name[i],end=' ') i+=1
P r i t h a
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)
Hello, and welcome to my world.
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:]}')
Hello, and welcome to my world.
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)
hello, and welcome to my world!
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='')
hello, and welcome to my world!
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)
2
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)
2
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
7
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)
15
Task:
for row in range(4): for col in range(7-(row*2)): print((col+1),end=" ") print()
1 2 3 4 5 6 7 1 2 3 4 5 1 2 3 1
for row in range(5): for col in range(5-row): print((row+1)+(col*2),end=" ") print()
1 3 5 7 9 2 4 6 8 3 5 7 4 6 5
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!