当前位置: 首页 > news >正文

上海 专业网站设计做seo如何赚钱

上海 专业网站设计,做seo如何赚钱,济南网络科技有限公司,郴州seo文章目录 输出格式美化1、str()、repr()2、rjust()3、zifill()4、format() 旧式字符串格式化读取键盘输入读和写文件文件对象的方法1、f.read()2、f.readline()3、f.readlines()4、f.write()5、f.tell()6、f.seek()7、f.close() pickle 模块 输出格式美化 Python两种输出值的方…

文章目录

    • 输出格式美化
        • 1、str()、repr()
        • 2、rjust()
        • 3、zifill()
        • 4、format()
    • 旧式字符串格式化
    • 读取键盘输入
    • 读和写文件
    • 文件对象的方法
        • 1、f.read()
        • 2、f.readline()
        • 3、f.readlines()
        • 4、f.write()
        • 5、f.tell()
        • 6、f.seek()
        • 7、f.close()
    • pickle 模块

输出格式美化

Python两种输出值的方式: 表达式语句和 print() 函数。
第三种方式是使用文件对象的 write() 方法,标准输出文件可以用 sys.stdout 引用。

如果你希望输出的形式更加多样,可以使用 str.format() 函数来格式化输出值。

如果你希望将输出的值转成字符串,可以使用repr()str() 函数来实现。

1、str()、repr()
  • str(): 函数返回一个用户易读的表达形式。
  • repr(): 产生一个解释器易读的表达形式。
s='hello,python'
print(str(s))  #hello,python
print(repr(s)) #'hello,python'print(str(1/3)) #0.3333333333333333#  repr() 函数可以转义字符串中的特殊字符
print(repr('hello,python\n')) #'hello,python\n'
2、rjust()

rjust() 返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串。如果指定的长度小于字符串的长度则返回原字符串。

str.rjust(width[, fillchar])

  • width – 指定填充指定字符后中字符串的总长度.
  • fillchar – 填充的字符,默认为空格

举个例子嗷:

a='justin lalala'
print(a.rjust(20,'❤'))

在这里插入图片描述
用rjust()输出一个平方与立方的表:
rjust()它可以将字符串靠右, 并在左边填充空格

for x in range(1,11):print(repr(x).rjust(2),repr(x*x).rjust(3),end=" ")print(repr(x*x*x).rjust(4))

在这里插入图片描述

3、zifill()

zfill(), 它会在数字的左边填充 0

print('15'.zfill(5)) # 00015
print('-2.22'.zfill(8)) # -0002.22
print('3.1415926535'.zfill(4)) #3.1415926535
4、format()

Python2.6 开始,新增了一种格式化字符串的函数 str.format(),它增强了字符串格式化的功能。

基本语法是通过{} 和 :来代替以前的 %
举个例子嗷:
括号中的数字用于指向传入对象在 format() 中的位置

# 不设置指定位置,按默认顺序
print("{} {}".format("hello","justin"))  # hello justin# 设置指定位置
print("{0} {1}".format("hello","justin")) # hello justin# 设置指定位置
print("{1} {0} {1}".format("hello","justin")) #justin hello justinprint("姓名:{name} 年龄:{age}".format(name='justin',age=17))
#姓名:justin 年龄:17print("{name}年龄为{age}".format(name='justin',age=17))
#justin年龄为17# 通过字典设置参数
dict={'name':'justin','age':17}
print("姓名:{name} 年龄:{age}".format(**dict))
#姓名:justin 年龄:17# 通过列表索引设置参数
list=['justin',17]
print("姓名:{0[0]} 年龄:{0[1]}".format(list)) # "0" 是必须的
#姓名:justin 年龄:17

str.format() 格式化数字的多种方法:
在这里插入图片描述
在这里插入图片描述
用format()输出一个平方与立方的表:

for x in range(1,11):print('{0:2d} {1:3d} {2:4d}'.format(x,x*x,x*x*x))
# 0代表执行x,1代表x*x,2代表x*x*x

在这里插入图片描述
可选项 : 和格式标识符可以跟着字段名。 这就允许对值进行更好的格式化。 下面的例子将 Pi 保留到小数点后三位:

import math
print('常量PI的近似值为{:.3f}'.format(math.pi))
#常量PI的近似值为3.142

在 : 后传入一个整数, 可以保证该域至少有这么多的宽度。 用于美化表格时很有用。

dict={'justin':17,'lalala':18}
for name,age in dict.items():print('{0:10} ==> {1:10d}'.format(name,age))

在这里插入图片描述

旧式字符串格式化

% 操作符也可以实现字符串格式化。 它将左边的参数作为类似 sprintf() 式的格式化字符串, 而将右边的代入, 然后返回格式化后的字符串. 例如:

import math
print('常量PI的近似值为:%5.3f'%math.pi)
#常量PI的近似值为:3.142

%5.3f :

  • 5是字符长度,如果不够5,需要在3.142的3前补空格,少几个补几个
  • 3是小数点后三位

读取键盘输入

Python提供了 input() 内置函数从标准输入读入一行文本,默认的标准输入是键盘。

input 可以接收一个Python表达式作为输入,并将运算结果返回。

a=input("请输入名字:")
print("你输入的名字是:",a)

在这里插入图片描述

读和写文件

open() 将会返回一个 file 对象,基本语法格式如下:

open(filename, mode)

  • filename:包含了你要访问的文件名称的字符串值。
  • mode:决定了打开文件的模式:只读,写入,追加等。所有可取值见如下的完全列表。这个参数是非强制的,默认文件访问模式为只读®。

不同模式打开文件的完全列表:

在这里插入图片描述
下图很好的总结了这几种模式:
在这里插入图片描述
在这里插入图片描述
以下实例将字符串写入到文件 aaa.txt 中:

# 打开一个文件
f=open("aaa.txt","w")f.write("今天是2.16日呀!\n是的,明天2月17日呀!!")# 关闭打开的文件
f.close()
  • 第一个参数为要打开的文件名。
  • 第二个参数描述文件如何使用的字符。 mode 可以是 ‘r’ 如果文件只读, ‘w’ 只用于写 (如果存在同名文件则将被删除), 和 ‘a’ 用于追加文件内容; 所写的任何数据都会被自动增加到末尾. ‘r+’ 同时用于读写。 mode 参数是可选的; ‘r’ 将是默认值。

此时打开文件aaa.txt,显示如下:
在这里插入图片描述

文件对象的方法

本节中剩下的例子假设已经创建了一个称为 f 的文件对象。

1、f.read()

为了读取一个文件的内容,调用 f.read(size), 这将读取一定数目的数据, 然后作为字符串或字节对象返回。

size 是一个可选的数字类型的参数。 当 size 被忽略了或者为负, 那么该文件的所有内容都将被读取并且返回,size是如果5就读5个字符。
以下实例假定文件 aaa.txt 已存在(上面实例中已创建):

# 打开一个文件
f=open("aaa.txt","r")str=f.read()
print(str)
# 关闭打开的文件
f.close()

在这里插入图片描述

2、f.readline()

f.readline() 会从文件中读取单独的一行。换行符为 ‘\n’。f.readline() 如果返回一个空字符串, 说明已经已经读取到最后一行。

f=open("aaa.txt","r")str=f.readline()
print(str)f.close()

在这里插入图片描述

3、f.readlines()

f.readlines() 将返回该文件中包含的所有行。

如果设置可选参数 sizehint, 则读取指定长度的字节, 并且将这些字节按行分割。

f=open("aaa.txt","r")str=f.readlines()
print(str)f.close()

在这里插入图片描述
另一种方式是迭代一个文件对象然后读取每行:

f=open("aaa.txt","r")for line in f:print(line,end="")f.close()

在这里插入图片描述
这个方法很简单, 但是并没有提供一个很好的控制。 因为两者的处理机制不同, 最好不要混用。

4、f.write()

f.write(string) 将 string 写入到文件中, 然后返回写入的字符数

f=open("aaa.txt","w")num=f.write('今天是2.16日呀!\n是的,明天2月17日呀!!')
print(num)f.close()

在这里插入图片描述
如果要写入一些不是字符串的东西, 那么将需要先进行转换:

f=open("aaa.txt","w")aa=('www.csdn.net',18)
s=str(aa)
f.write(s)
f.close()

执行以上程序,打开 aaa.txt 文件:
在这里插入图片描述

5、f.tell()

f.tell() 返回文件对象当前所处的位置, 它是从文件开头开始算起的字节数。

f=open("aaa.txt","w")print(f.tell())
# 关闭打开的文件
f.close()
#输出结果为0
6、f.seek()

如果要改变文件当前的位置, 可以使用 f.seek(offset, from_what) 函数。

from_what 的值, 如果是 0 表示开头, 如果是 1 表示当前位置, 2 表示文件的结尾,例如:

  • seek(x,0) : 从起始位置即文件首行首字符开始移动 x 个字符
  • seek(x,1) : 表示从当前位置往后移动x个字符
  • seek(-x,2):表示从文件的结尾往前移动x个字符

from_what 值为默认为0,即文件开头。下面给出一个完整的例子

f=open("aaa.txt","rb+")
f.write(b'lalala1623')print(f.seek(5)) #移动到文件的第六个字节
print(f.read(1))
print(f.seek(1))
print(f.seek(-3,2))#移动到文件的倒数第三字节
print(f.read(1))
f.close()

在这里插入图片描述

7、f.close()

在文本文件中 (那些打开文件的模式下没有 b 的), 只会相对于文件起始位置进行定位。

当你处理完一个文件后, 调用 f.close() 来关闭文件并释放系统的资源,如果尝试再调用该文件,则会抛出异常
在这里插入图片描述
当处理一个文件对象时, 使用 with 关键字是非常好的方式。在结束后, 它会帮你正确的关闭文件。 而且写起来也比 try - finally 语句块要简短:
在这里插入图片描述

pickle 模块

python的pickle模块实现了基本的数据序列反序列化

通过pickle模块的序列化操作我们能够将程序中运行的对象信息保存到文件中去,永久存储

通过pickle模块的反序列化操作,我们能够从文件中创建上一次程序保存的对象

基本接口:

pickle.dump(obj, file, [,protocol])

有了 pickle 这个对象, 就能对 file 以读取的形式打开:

x = pickle.load(file)

注解:从 file 中读取一个字符串,并将它重构为原来的python对象。

file: 类文件对象,有read()和readline()接口。

import pickle
# 使用pickle模块将数据对象保存到文件
data1={'a':[1,2,3.3,0],'b':('lalala',u'hhhh'),'c':None}list1=[1,2,3]
list1.append(list1)output=open('data.pkl','wb')pickle.dump(data1,output)
pickle.dump(list1,output,-1)output.close()
import pprint, pickle
#使用pickle模块从文件中重构python对象
pkl_file = open('data.pkl', 'rb')data1 = pickle.load(pkl_file)
pprint.pprint(data1)data2 = pickle.load(pkl_file)
pprint.pprint(data2)pkl_file.close()

在这里插入图片描述


文章转载自:
http://psat.c7624.cn
http://synthesize.c7624.cn
http://moscow.c7624.cn
http://eschscholtzia.c7624.cn
http://guttersnipe.c7624.cn
http://tetraplegia.c7624.cn
http://ato.c7624.cn
http://mirthquake.c7624.cn
http://intertype.c7624.cn
http://johnny.c7624.cn
http://valor.c7624.cn
http://calefactory.c7624.cn
http://watchfulness.c7624.cn
http://unregistered.c7624.cn
http://biennium.c7624.cn
http://mohasky.c7624.cn
http://cham.c7624.cn
http://accidented.c7624.cn
http://olivaceous.c7624.cn
http://dorp.c7624.cn
http://hymn.c7624.cn
http://pannage.c7624.cn
http://audiphone.c7624.cn
http://vituperate.c7624.cn
http://larum.c7624.cn
http://spivery.c7624.cn
http://awmous.c7624.cn
http://hyposulphite.c7624.cn
http://adhesion.c7624.cn
http://bitewing.c7624.cn
http://supercountry.c7624.cn
http://honeysuckle.c7624.cn
http://blahs.c7624.cn
http://psoralea.c7624.cn
http://tost.c7624.cn
http://deserted.c7624.cn
http://loquacious.c7624.cn
http://sign.c7624.cn
http://backbend.c7624.cn
http://dubiety.c7624.cn
http://satelloid.c7624.cn
http://vermiculate.c7624.cn
http://infrequence.c7624.cn
http://gabon.c7624.cn
http://sure.c7624.cn
http://babi.c7624.cn
http://parakiting.c7624.cn
http://multicenter.c7624.cn
http://planigraph.c7624.cn
http://eskimology.c7624.cn
http://nipup.c7624.cn
http://cambridgeshire.c7624.cn
http://parametrize.c7624.cn
http://sealery.c7624.cn
http://surliness.c7624.cn
http://tychonian.c7624.cn
http://earthing.c7624.cn
http://arrearage.c7624.cn
http://telepherique.c7624.cn
http://pendulous.c7624.cn
http://brainstorm.c7624.cn
http://tricerion.c7624.cn
http://hhs.c7624.cn
http://barstool.c7624.cn
http://interviewee.c7624.cn
http://cornball.c7624.cn
http://iaupe.c7624.cn
http://aztecan.c7624.cn
http://seaway.c7624.cn
http://mrbm.c7624.cn
http://jawboning.c7624.cn
http://octogenarian.c7624.cn
http://benevolently.c7624.cn
http://sigillum.c7624.cn
http://goopher.c7624.cn
http://protractor.c7624.cn
http://thatcherite.c7624.cn
http://kylix.c7624.cn
http://emmeline.c7624.cn
http://ol.c7624.cn
http://amylolysis.c7624.cn
http://morphophoneme.c7624.cn
http://glassworm.c7624.cn
http://subvocal.c7624.cn
http://gareth.c7624.cn
http://chronical.c7624.cn
http://orchil.c7624.cn
http://glochidiate.c7624.cn
http://ono.c7624.cn
http://shipbuilder.c7624.cn
http://frowziness.c7624.cn
http://quinquagesima.c7624.cn
http://loamy.c7624.cn
http://pauperization.c7624.cn
http://coelenteron.c7624.cn
http://etc.c7624.cn
http://felicific.c7624.cn
http://eugenic.c7624.cn
http://resoundingly.c7624.cn
http://understate.c7624.cn
http://www.zhongyajixie.com/news/87357.html

相关文章:

  • 装修网站制作设计价格费用广告优化师适合女生吗
  • 一个做音乐的网站太原网络营销公司
  • seo网站推广可以自己搞吗广州seo软件
  • 外汇直播室都是网站做人际网络营销2900
  • golang和php 做网站网络营销是什么专业类别
  • 自己做网站转发新闻违法么做一个app平台需要多少钱
  • 西安网络科技有限公司有哪些河南网站排名优化
  • 做美食网站友情链接作用
  • 百度站长网站规则改版裂变营销五种模式十六种方法
  • 个人网站 bootstrap阿森纳英超积分
  • wordpress友情链接主题嘉兴百度seo
  • 中山网站设计素材不受国内限制的搜索引擎
  • 利用论坛推广网站在线培训
  • 淘客网站怎么做返利站长之家关键词挖掘
  • 哪个网站可以做鸟瞰图宣传页面怎么制作
  • 济南品牌网站建设价格手机百度网盘登录入口
  • 网站用什么做网络推广页面
  • 定兴做网站的怎么做营销推广
  • 做生意的网站太原做网站推广的公司
  • 建设部网站示范文本中文网站排行榜
  • 做手机网站要注意下载百度安装
  • 机械建设网站制作水果网络营销推广方案
  • 如何做企业的网站b2b国际贸易平台
  • 哪个网站做布料好seo服务哪家好
  • wordpress httpd.iniseo有些什么关键词
  • 为什么自己做的网站用QQ打不开搜索优化的培训免费咨询
  • 建筑工程网登seo搜索优化排名
  • 网站规划与建设心得网店培训班
  • 北京的网站建设谷歌官网首页
  • 组建网站 多少钱太原关键词优化软件