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

聖光之護
发布: 2024-12-10 08:00:02
转载
684人浏览过

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 
登录后复制

以上就是Day - CSV 文件、ASCII、字符串方法的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号