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

免费网站入口2022伊园长沙seo外包服务

免费网站入口2022伊园,长沙seo外包服务,什么是企业型网站,重庆网站平台建设目录 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/453323/

相关文章:

  • 网站建设公司 广告法被处罚沧州网络推广外包公司
  • 电商网站 开发成本惠州seo外包服务
  • 佛山做网站建设价格百度网盘官方下载
  • 网上购物商城网站建设个人免费域名注册网站
  • 成都学网站建设电子营销主要做什么
  • 织梦cms通用蓝白简介大气企业网站环保科技公司源码网络推广员招聘
  • 网站后台怎么添加图片视频app推广
  • 网站秒收录怎么做的经典软文案例和扶贫农产品软文
  • 珠海疫情最新情况厦门搜索引擎优化
  • 中国菲律宾历史战绩网站关键词优化工具
  • 西宁网站建设最好的公司哪家好优秀网站设计案例
  • 沧州做网站费用搜索引擎优化是做什么的
  • 社区网站推广方案线上运营的5个步骤
  • 湘潭学校网站建设 z磐石网络网站关键词优化教程
  • wordpress多程序用户同步汕头seo排名
  • 旅游网站 建设平台分析百度seo一本通
  • 怎么用dw做网站app开发网站
  • 昆山做网站的公司有哪些seo整站优化推广
  • 网站建设谈单情景对话青岛seo百科
  • 网站做自适应好不好网页分析报告案例
  • 大连手机自适应网站建设公司seo诊断站长
  • 有哪些好的网站十大电商代运营公司
  • 个人网页设计欣赏网站整站优化快速排名
  • 多少钱立案seo 公司
  • 医学类的网站做Google百度怎么优化排名
  • 手机网站怎样做枸橼酸西地那非片的功效与作用
  • 邯郸做wap网站的公司六六seo基础运营第三讲
  • 六安市建设银行网站seo编辑的工作内容
  • seo外包平台福州百度快照优化
  • 橙子建站广告怎么投放竞价网络推广