搜尋
首頁後端開發Python教學Day - CSV 檔案、ASCII、字串方法

Day - CSV file, ASCII, String methods

CSV(逗號分隔值):

CSV 檔案代表一行,行內的每個值都用逗號分隔。
CSV 檔案看起來像 Excel,但 Excel 檔案只能在 Excel 軟體中開啟。
CSV 檔案用於所有作業系統。

我們可以開啟以下兩種格式的CSV檔案。

f =open("sample.txt", "r")

with open("sample.txt",’r’) as f:

r-讀
開啟檔案進行讀取。文件必須存在。
w-寫
開啟檔案進行寫入。建立一個新文件或覆蓋現有文件。
rb-讀取二進位檔
這用於讀取二進位文件,如圖像、視訊、音訊檔案、PDF 或任何非文字檔案。

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 代表美國資訊交換標準代碼。

ASCII 表:
48-57 - 數字(數字 0 到 9)
65-90 - A-Z(大寫字母)
97-122 - a-z(小寫字母)

使用 ASCII 表的模式程式:

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 

使用 for 迴圈:

name = 'pritha'
for letter in name:
    print(letter,end=' ')

P r i t h a

使用 while 迴圈:

name = 'pritha'
i=0
while i<len print i>





<pre class="brush:php;toolbar:false">P r i t h a

字串方法:
1.大寫()
Python中的capitalize()方法用於將字串的第一個字元轉換為大寫,並將所有其他字元轉換為小寫。

txt = "hello, and welcome to my world."
x = txt.capitalize()
print (x)

Hello, and welcome to my world.

使用 ASCII 表編寫大小寫程式:

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()
Python 中的 casefold() 方法用於將字串轉換為小寫。

txt = "Hello, And Welcome To My World!"
x = txt.casefold()
print(x)
hello, and welcome to my world!

使用 ASCII 表編寫一個摺頁程式:

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()
Python 中的 count() 方法用於統計字串中子字串的出現次數。

txt = "I love apples, apple is my favorite fruit"
x = txt.count("apple")
print(x)

2

為給定的鍵寫一個計數程式:

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

寫一個程式來找出給定鍵的第一次出現:

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

寫一個程式來最後一次出現給定的鍵:

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

任務:

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 

以上是Day - CSV 檔案、ASCII、字串方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
列表和陣列之間的選擇如何影響涉及大型數據集的Python應用程序的整體性能?列表和陣列之間的選擇如何影響涉及大型數據集的Python應用程序的整體性能?May 03, 2025 am 12:11 AM

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

說明如何將內存分配給Python中的列表與數組。說明如何將內存分配給Python中的列表與數組。May 03, 2025 am 12:10 AM

Inpython,ListSusedynamicMemoryAllocationWithOver-Asalose,而alenumpyArraySallaySallocateFixedMemory.1)listssallocatemoremoremoremorythanneededinentientary上,respizeTized.2)numpyarsallaysallaysallocateAllocateAllocateAlcocateExactMemoryForements,OfferingPrediCtableSageButlessemageButlesseflextlessibility。

您如何在Python數組中指定元素的數據類型?您如何在Python數組中指定元素的數據類型?May 03, 2025 am 12:06 AM

Inpython,YouCansspecthedatatAtatatPeyFelemereModeRernSpant.1)Usenpynernrump.1)Usenpynyp.dloatp.dloatp.ploatm64,formor professisconsiscontrolatatypes。

什麼是Numpy,為什麼對於Python中的數值計算很重要?什麼是Numpy,為什麼對於Python中的數值計算很重要?May 03, 2025 am 12:03 AM

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

討論'連續內存分配”的概念及其對數組的重要性。討論'連續內存分配”的概念及其對數組的重要性。May 03, 2025 am 12:01 AM

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

您如何切成python列表?您如何切成python列表?May 02, 2025 am 12:14 AM

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

在Numpy陣列上可以執行哪些常見操作?在Numpy陣列上可以執行哪些常見操作?May 02, 2025 am 12:09 AM

numpyallowsforvariousoperationsonArrays:1)basicarithmeticlikeaddition,減法,乘法和division; 2)evationAperationssuchasmatrixmultiplication; 3)element-wiseOperations wiseOperationswithOutexpliitloops; 4)

Python的數據分析中如何使用陣列?Python的數據分析中如何使用陣列?May 02, 2025 am 12:09 AM

Arresinpython,尤其是Throughnumpyandpandas,weessentialFordataAnalysis,offeringSpeedAndeffied.1)NumpyArseNable efflaysenable efficefliceHandlingAtaSetSetSetSetSetSetSetSetSetSetSetsetSetSetSetSetsopplexoperationslikemovingaverages.2)

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能