Python爬虫-第一章-2-基础语法

时间:2022-12-19 11:19:51
  1. 文件操作
# Demo Describe:文件操作
'''
The argument mode points to a string beginning with one of the following
sequences (Additional characters may follow these sequences.):

``r'' Open text file for reading. The stream is positioned at the
beginning of the file.

``r+'' Open for reading and writing. The stream is positioned at the
beginning of the file.

``w'' Truncate file to zero length or create text file for writing.
The stream is positioned at the beginning of the file.

``w+'' Open for reading and writing. The file is created if it does not
exist, otherwise it is truncated. The stream is positioned at
the beginning of the file.

``a'' Open for writing. The file is created if it does not exist. The
stream is positioned at the end of the file. Subsequent writes
to the file will always end up at the then current end of file,
irrespective of any intervening fseek(3) or similar.

``a+'' Open for reading and writing. The file is created if it does not
exist. The stream is positioned at the end of the file. Subse-
quent writes to the file will always end up at the then current
end of file, irrespective of any intervening fseek(3) or similar.


'''

try:
# =================示例1,简单文件读取==========
'''
open(路径,操作模式【只读或者编辑】,内容字节码)
路径:
1,绝对路径 2,相对路径(‘../’表示返回上一层)
'''
# fileContent = open('../FileForDemo/P1_fileOperate.txt',mode='r',encoding='utf-8') #读取txt文件时建议带上转码encoding
# print(fileContent.read()) #全部读取,文件内容量大时不推荐
# print(fileContent.readlines()) #读取文件内容包括隐藏的操作符,以列表展示
# print(fileContent.readline().strip()) #读取一行数据,并去掉行收尾的换行符

# ----------------2.常用文件读取方式----------------------------
# for line in fileContent:
# print(line.strip()) #通过单行读取防止一次性读取数据量过载发生错误

# =================示例2,简单文件写入==========
# fileContent = open('../FileForDemo/P1_fileOperate.txt', mode='w', encoding='utf-8') #'w' 重写
lst = ['你好', '你好2', '你好3', '你好4', ]
# for item in lst:
# fileContent.write(item)
# fileContent.write('\n')

# fileContent = open('../FileForDemo/P1_fileOperate.txt', mode='a', encoding='utf-8') # 'w' append 追加
# for item in lst:
# fileContent.write(item)
# fileContent.write('\n')

# fileContent.close() #以上情况需要手动关闭文件
# =================示例3,简单文件读取 with==========
# with open('../FileForDemo/P1_fileOperate.txt',mode='r',encoding='utf-8') as fileContent: #with I/O操作后会自动关闭
# for line in fileContent:
# print(line.strip())

# =================示例4,简单图片文件复制==========
'''
1,b : bytes 多字节 非文本文件时,不添加转码(其内容的存储格式一般都是原始字节)
2,Python 是逐行运行,换行拼接代码时加 ‘\’
'''
with open('../FileForDemo/JPNoodle.jpg', mode='rb') as fileContent, \
open('../FileForDemo/JPNoodle(1)).jpg', mode='wb') as fileContent1:
for pic in fileContent:
fileContent1.write(pic)

# =================示例5,简单文件修改==========
'''
文件I/O操作修改内容的一般处理方式:
从原文件中逐行读取数据到内存,在内存中进行修改,将改动的内容添加到新文件中,当数据全部修改完毕,删除源文件并将新文件名改为源文件名称(后台替换)
'''
import os # 包含系统文件操作命令

with open('../FileForDemo/P1_fileOperate.txt', mode='r', encoding='utf-8') as fileContent_origin, \
open('../FileForDemo/P1_fileOperate_copy.txt', mode='w', encoding='utf-8') as fileContent_copy:
for line in fileContent_origin:
line_copy = line.strip()
if '问:人工智能是不是走错了方向?' in line_copy:
line_copy = line_copy.replace('问:人工智能是不是走错了方向?',
'人工智能方向是否有问题?') # 对内存中文件数据进行修改 注意str不能被更改,需要复制给其他对象进行更新
fileContent_copy.write(line_copy) # 写入新文件
fileContent_copy.write('\n')
os.remove('../FileForDemo/P1_fileOperate.txt')
os.rename('../FileForDemo/P1_fileOperate_copy.txt', '../FileForDemo/P1_fileOperate.txt')

with open('../FileForDemo/P1_fileOperate.txt', mode='r', encoding='utf-8') as fileContent:
for line in fileContent:
print(line.strip())

except ZeroDivisionError as e:
print(e)
  1. for循环
# Demo Describe:for循环

# # ===========实例1,循环输出字符(tip:字符串可迭代)==========
# a='这是一段代码!'
# for c in a:
# print(c)
# '''
# 输出效果如下
# 这
# 是
# 一
# 段
# 代
# 码
# !
# '''

# # ===========实例2,循环输出1至10(包含10)==========
# '''
# range(n) => 指定 0-n 的范围
# '''
# for c in range(11):
# print(c)

# # ===========实例3,循环输出3至10(不包含10)==========
# '''
# range(m,n) => 指定 m-n 的范围
# '''
# for c in range(3,10):
# print(c)

# ===========实例4,循环输出1至10(不包含10),并且间隔2个数==========
'''
range(m,n,s) => 指定 m-n 的范围,间隔s
下面代码实现效果
1
3
5
7
9
'''
# # while实现方式
# i=1
# while i<=10:
# print(i)
# i=i+2
# for实现方式
for c in range(1, 10, 2):
print(c)

3.if条件判断

# Demo Describe:if条件判断

# # =====示例1====== 假设需要500元支付账单
# a = input('请输入支付金额:')
# if int(a)>500:
# print('支付成功!')
# else:
# print('支付金额不足,请补足')


# # =====示例2====== 假设支付账单达到一定金额送洗面奶
# money = int(input('请输入支付金额:'))
# if money > 500:
# if money > 1000:
# print('支付成功!获赠洗面奶一瓶。')
# else:
# print('支付成功!')
# else:
# print('支付金额不足,请补足')

# =====示例3====== 假设支付账单达到一定金额送洗面奶
money = int(input('请输入支付金额:'))
if money > 1000:
print('支付成功!获赠洗面奶一瓶。')
elif money > 500:
print('支付成功!')
else:
print('支付金额不足,请补足')