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

地板网站源码推广网站有哪些

地板网站源码,推广网站有哪些,苏州做网站推广的,自己做网站吗目录 1.从文件中读取数据 2.写入文件 3.存储数据 3.1使用json.dump()和json.load() 3.2保存和读取用户生成的数据 3.3重构 1.从文件中读取数据 读取整个文件 with open(data.txt) as file_object: contents file_object.read()print(contents)print(contents.rstrip…

目录

1.从文件中读取数据

2.写入文件

3.存储数据

3.1使用json.dump()和json.load()

3.2保存和读取用户生成的数据

3.3重构


1.从文件中读取数据

读取整个文件

with open('data.txt') as file_object:
    contents = file_object.read()
print(contents)
print(contents.rstrip()) #删除字符串末尾空白

文件路径

with open('text_files//data.txt') as file_object:

file_path = '/home/eh/data.txy'
with open(file_path) as file_object:

逐行读取

with open(file_path) as file_object:
    for line in file_path:
        print(line.rstrip())

创建一个包含

文件各行内容的列表

filename ='data.txt'
with open(filename) as file_object:
    lines = file_object.readlines()
for line in lines:
    print(line.rstrip())

使用文件内容

data_string = ''
for line in lines:
    data_string += line.rstrip() #循环加入data_string
print(data_string)

打印圆周率小数点后10位数

3.1415926535…

print(f"{data_string[:12]}")

数列中是否包含这个字段

……

my_data = input("输入数字,字符串是否有这个数:")
if my_data in data_string:
    print("你的数字在这个字符串中")
else:
    print("你的数字不在这个字符串中")

2.写入文件

写入空文件

file = 'abc.txt'
with open(file) as file_object:
    file_object.write("我是一个蘑菇。")

写入多行open(file, 'w')

with open(file, 'w') as file_object:
    file_object.write("我是一个蘑菇。\n")
    file_object.write("我不是一个蘑菇。\n")

附加到文件open(file, 'a')

with open(file, 'a') as file_object:
    file_object.write("我是一个蘑菇。\n")

3.存储数据

json能够将简单的python数据结构转储到文件中

3.1使用json.dump()和json.load()

json.dump()

可以接受两个实参

import json

number =[2,3,5]
filename = 'number.json'
with open(filename, 'w') as f:
    json.dump(number, f)

number.json文件中:

[2, 3, 5]

json.load()

将列表

读取到内存中

with open(filename, 'w') as f:
    numbers = json.load(f)
print(numbers)

3.2保存和读取用户生成的数据

代码

输出

username.json文件

存储用户

import json

username = input("你的名字:")
filename = 'username.json'
with open(filename, 'w') as f:
    json.dump(username, f)
    print(f"Hello,{username}!")

你的名字:Ann

Hello,Ann!

"Ann"

向已存储的用户

发出问候

import json
filename = 'username.json'
with open(filename) as f:
    username = json.load(f)
    print(f"欢迎回来,{username}!")

欢迎回来,Ann!

如用户已存储,加载他。如果没有,则提示用户输入并存储。

import json

filename = 'username.json'
try:
    with open(filename) as f:    #尝试打开username.json
        username = json.load(f)    #如果username.json存在
except FileNotFoundError:    #username.json不存在
    username = input("你的名字:")
    with open(username, 'w') as f:
        json.dump(username, f)    #存储
        print(f"Hello,{username}!")
else:
    print(f"欢迎回来,{username}!")

3.3重构

重构:代码能够正常运行,但通过将其划分为一系列完成具体工作的函数,还可以进行改进。

作用:让代码更清晰、更利于理解、更容易扩展。

http://www.hkea.cn/news/967556/

相关文章:

  • 淘宝客网站免费做seo网站关键词优化机构
  • 企业做网站建设的好处seo网站关键词优化
  • 一般网站用什么做的最新新闻国内大事件
  • 做线上网站需要钱吗互联网营销推广
  • 找个美工做淘宝网站需要多少钱南昌seo方案
  • 网站用户登录流程图外贸高端网站设计公司
  • 做搜狗手机网站优化软代写
  • wordpress页面背景颜色win7优化设置
  • 做分类信息网站代码百度搜索推广优化师工作内容
  • 南京网站开发公司关键词推广
  • 合水口网站建设百度指数明星人气榜
  • 上传网站图片处理推广软件免费
  • 做网站怎么写代码下载百度软件
  • 县城做网站网站搭建关键词排名
  • b2b多平台一键发布seo需要掌握哪些技术
  • 网站建设推广合同网络广告联盟
  • 汽车网站正在建设中模板什么是营销模式
  • 宜昌seo百度seo优化
  • 做网站公司q房网seo快速排名站外流量推广
  • 南宁网站排名优化广州发布紧急通知
  • 网站建设的策划方案seo排名
  • 网站模板绑定域名培训班
  • coupang入驻条件2022台州关键词优化报价
  • 网站建设前景怎么样google优化师
  • 上海免费网站建设淘宝引流推广怎么做
  • 单位网站建设目的西安网站建设公司排行榜
  • 福州制作网站软件无人在线观看高清视频单曲直播
  • 建设银行卡网站百度账号登录个人中心
  • 网站显示500错误怎么解决方法seo网站推广排名
  • 广告免费设计在线生成网站排名优化