search
HomeBackend DevelopmentPython TutorialDay - CSV file, ASCII, String methods

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:

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 print i>





<pre class="brush:php;toolbar:false">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





<pre class="brush:php;toolbar:false">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 if txt count start end else: print>





<pre class="brush:php;toolbar:false">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 if txt print break start end>





<pre class="brush:php;toolbar:false">
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 if txt final="start" start end else: print>





<pre class="brush:php;toolbar:false">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!

Statement
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
How does the choice between lists and arrays impact the overall performance of a Python application dealing with large datasets?How does the choice between lists and arrays impact the overall performance of a Python application dealing with large datasets?May 03, 2025 am 12:11 AM

ForhandlinglargedatasetsinPython,useNumPyarraysforbetterperformance.1)NumPyarraysarememory-efficientandfasterfornumericaloperations.2)Avoidunnecessarytypeconversions.3)Leveragevectorizationforreducedtimecomplexity.4)Managememoryusagewithefficientdata

Explain how memory is allocated for lists versus arrays in Python.Explain how memory is allocated for lists versus arrays in Python.May 03, 2025 am 12:10 AM

InPython,listsusedynamicmemoryallocationwithover-allocation,whileNumPyarraysallocatefixedmemory.1)Listsallocatemorememorythanneededinitially,resizingwhennecessary.2)NumPyarraysallocateexactmemoryforelements,offeringpredictableusagebutlessflexibility.

How do you specify the data type of elements in a Python array?How do you specify the data type of elements in a Python array?May 03, 2025 am 12:06 AM

InPython, YouCansSpectHedatatYPeyFeLeMeReModelerErnSpAnT.1) UsenPyNeRnRump.1) UsenPyNeRp.DLOATP.PLOATM64, Formor PrecisconTrolatatypes.

What is NumPy, and why is it important for numerical computing in Python?What is NumPy, and why is it important for numerical computing in Python?May 03, 2025 am 12:03 AM

NumPyisessentialfornumericalcomputinginPythonduetoitsspeed,memoryefficiency,andcomprehensivemathematicalfunctions.1)It'sfastbecauseitperformsoperationsinC.2)NumPyarraysaremorememory-efficientthanPythonlists.3)Itoffersawiderangeofmathematicaloperation

Discuss the concept of 'contiguous memory allocation' and its importance for arrays.Discuss the concept of 'contiguous memory allocation' and its importance for arrays.May 03, 2025 am 12:01 AM

Contiguousmemoryallocationiscrucialforarraysbecauseitallowsforefficientandfastelementaccess.1)Itenablesconstanttimeaccess,O(1),duetodirectaddresscalculation.2)Itimprovescacheefficiencybyallowingmultipleelementfetchespercacheline.3)Itsimplifiesmemorym

How do you slice a Python list?How do you slice a Python list?May 02, 2025 am 12:14 AM

SlicingaPythonlistisdoneusingthesyntaxlist[start:stop:step].Here'showitworks:1)Startistheindexofthefirstelementtoinclude.2)Stopistheindexofthefirstelementtoexclude.3)Stepistheincrementbetweenelements.It'susefulforextractingportionsoflistsandcanuseneg

What are some common operations that can be performed on NumPy arrays?What are some common operations that can be performed on NumPy arrays?May 02, 2025 am 12:09 AM

NumPyallowsforvariousoperationsonarrays:1)Basicarithmeticlikeaddition,subtraction,multiplication,anddivision;2)Advancedoperationssuchasmatrixmultiplication;3)Element-wiseoperationswithoutexplicitloops;4)Arrayindexingandslicingfordatamanipulation;5)Ag

How are arrays used in data analysis with Python?How are arrays used in data analysis with Python?May 02, 2025 am 12:09 AM

ArraysinPython,particularlythroughNumPyandPandas,areessentialfordataanalysis,offeringspeedandefficiency.1)NumPyarraysenableefficienthandlingoflargedatasetsandcomplexoperationslikemovingaverages.2)PandasextendsNumPy'scapabilitieswithDataFramesforstruc

See all articles

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

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use