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:
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
From another module:
import csv f =open("score.csv", "r") csv_reader = csv.reader(f) for row in csv_reader: print(row) f.close()
Output:
['Player', 'Score'] ['Virat', '80'] ['Rohit', '90'] ['Dhoni', '100']
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()
Output:
A A B A B C A B C D A B C D E
2)
for row in range(5): for col in range(5-row): print(chr(row+65), end=' ') print()
Output:
A A A A A B B B B C C C D D E
Printing name using for loop and while loop:
Method-1:
name = 'guru' for letter in name: print(letter,end=' ')
Method-2:
name = 'guru' i = 0 while i<len(name): print(name[i],end=' ') i+=1
Output:
g u r u
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:]}")
Output:
Hello, and welcome to my world.
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='')
Output:
guruprasanna!
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)
Output:
2
#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
Output:
7
#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)
Output:
15
Task:
Find program for given output:
1 2 3 4 5 6 7 1 2 3 4 5 1 2 3 1
Input:
for row in range(4): for col in range(7-(row*2)): print((col+1), end=' ') print()
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!