Day - CSV 文件、ASCII、字符串方法

ID:21912 / 打印

day - csv 文件、ascii、字符串方法

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(name):     print(name[i],end=' ')     i+=1 
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<'z':         letter = ord(letter)+32         letter = chr(letter)     print(letter,end='')  
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(txt):     if txt[start:end]==key:         count+=1     start+=1     end+=1 else:     print(count) 
2 

编写一个程序来查找给定键的第一次出现:

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 

编写一个程序来最后一次出现给定的键:

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 

任务:

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  
上一篇: Python 缓存:如何通过有效的缓存来加速代码
下一篇: Python 中处理错误的最佳实践

作者:admin @ 24资源网   2025-01-14

本站所有软件、源码、文章均有网友提供,如有侵权联系308410122@qq.com

与本文相关文章

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。