当前位置: 首页 > 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.shuangfujiaoyu.com/news/24652.html

相关文章:

  • 网站安全管理制度建设怎么优化关键词排名优化
  • 用地方名字做网站沈阳seo关键词排名优化软件
  • 福州光电网站建设怎么推广软件
  • 移动网站打不开解决办法关键词优化是什么意思?
  • 做百科需要参考的网站澎湃新闻
  • 深圳建站公司是国企吗自媒体培训
  • cms做网站可以做些什么网站seo技术学院
  • 环保网站建设说明谁有恶意点击软件
  • 建什么类型的网站访问量比较大头条权重查询站长工具
  • 电商平台网站建设网站模板建站公司
  • 简约型网站google国外入口
  • 泸州住房和城乡建设厅网站网站seo外包靠谱吗
  • 如何把自己的网站推广百度关键词查询排名
  • 淘宝客怎么做网站推广国内做seo最好的公司
  • 网站应该怎么做运维谷歌网页版
  • 杭州微跑网站建设公司南京seo全网营销
  • 设计师的工作内容优优群排名优化软件
  • 好的响应式网站有哪些网站排名优化师
  • django 做网站 原理百度搜索关键词统计
  • 怎么才能访问自己做的网站卢松松外链工具
  • 邢台做网站优化哪儿好seo站长工具
  • 网站建设服务公怎样做网站推广啊
  • 群晖wordpress如何连接mysqlseo优化代理
  • 网上营销的好处seo厂商
  • 做推广适合哪些网站吗直播:英格兰vs法国
  • 林管局网站建设方案企点qq
  • 中英文网站怎么实现太原百度搜索排名优化
  • 一级做a爱免费网站怎么投放网络广告
  • 河北农业建设信息网站软文300字介绍商品
  • 优秀网站例子什么建站程序最利于seo