读取和写文件

这篇博客介绍了Python中如何进行文件操作,包括读取文本和二进制文件,文件复制,以及读取和写入CSV文件。还提到了通过URL下载图片,使用UUID以及联网获取API数据并保存到CSV文件中的实践。
摘要由CSDN通过智能技术生成

文件操作

读取文本文件(字符文件)

file = open('resources/致橡树.txt', 'r', encoding='utf-8')
try:
    # 如果读不到数据,read方法会返回None
    data = file.read(32)
    while data:
        print(data, end='')
        data = file.read(32)
finally:
    file.close()

之前都是直接输出在pycharm运行系统下面,现在是把这些输出到一个新的文件下面。
finally —> 总是执行代码(不管正常异常,finally中的代码一定会被执行到)

读取二进制文件(字节文件)

from io import SEEK_END, SEEK_SET

file = open(file='resources/guido.jpg', mode='rb')
# 如果希望获得文件的字节数,可以先用seek方法将文件指针移动到文件末尾
# 然后通过tell方法获取文件指针移动的字节数,这个字节数就是文件的大小
file.seek(0, SEEK_END)
print(file.tell())
# 将文件指针移动到文件最开始的位置
file.seek(0, SEEK_SET)
try:
    data = file.read(512)
    while data:
        print(data, end='')
        data = file.read(512)
finally:
    file.close()

文件复制

def file_copy(source_file, target_file):
    """文件拷贝"""
    with open(source_file, 'rb') as source:
        with open(target_file, 'wb') as target:
            data = source.read(512)
            while data:
                target.write(data)
                data = source.read(512)


if __name__ == '__main__':
    file_copy('resources/guido.jpg', '/Users/Hao/Desktop/guido.jpg')

如果要将100以内的质数输出到文件中每行一个数。

def is_prime(num):
    for i in range(2, int(num ** 0.5) + 1):
        if num % i == 0:
            return False
    return True


def main():
    with open('resources/prime.txt', 'w') as file:
        for num in range(2, 100):
            if is_prime(num):
                # file.write(f'{num}\r\n')
                print(num, file=file)


if __name__ == '__main__':
    main()
从文件中读入体温测量数据,显示体温不正常的病人的信息。
37.2以下 —> 正常
37.2~38.5 —> 发热
38.5以上 —> 高热
import csv

with open('resources/temperature.txt') as file1:
    with open('resources/result.csv', 'w', encoding='gbk', newline='') as file2:
        writer = csv.writer(file2, delimiter='|')
        # writerow方法的参数是一个列表或元组(代表一行中所有的数据,默认用逗号分隔)
        writer.writerow(['ID', 'temperature', 'information'])
        content = file1.readline()
        while content:
            no, temp = content.split()
            temp = float(temp)
            if temp >= 37.2:
                if temp <= 38.5:
                    info = '发热'
                else:
                    info = '高热'
                writer.writerow([no, temp, info])
            content = file1.readline()

读取CSV(逗号分隔值)文件 —> Comma Seperated Value

import csv

with open('resources/2018年北京积分落户数据2.csv', encoding='gb18030') as file:
    # delimiter ---> 设置分隔符(默认是英文的逗号)
    # quotechar ---> 包裹字符串的符号(默认是英文的双引号)
    reader = csv.reader(file, delimiter='#', quotechar='|')
    for row in reader:
        print(row)
    # data = file.readline().replace('\n', '').replace('|', '')
    # while data:
    #     print(data.split('#'))
    #     data = file.readline().replace('\n', '').replace('|', '')

通过URL下载图片

# import requests
#
# resp = requests.get('http://p6.toutiaoimg.com/img/pgc-image/SVlm41hHScGcOO~tplv-tt-cs0:850:285.jpg')
# with open('resources/temp.png', 'wb') as file:
#     file.write(resp.content)

# from example12 import download_picture
#
# download_picture(
#     'https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png',
#     'resources/baidu.png'
# )

import uuid

for _ in range(10):
    print(uuid.uuid1().hex)

UUID —> Universal Unique IDentifier

联网获取API接口的数据,将数据写入CSV文件并下载对应的图片资源

import csv
import os
import uuid

import requests

if not os.path.exists('resources/images'):
    os.makedirs('resources/images')

# def get_filename(picture_url):
#     """通过URL获取文件名"""
#     pos = picture_url.rfind('/')
#     return picture_url[pos + 1:].replace('~', '_').replace(':', '_')


def download_picture(picture_url, filename):
    """下载图片"""
    resp = requests.get(picture_url)
    with open(filename, 'wb') as file:
        file.write(resp.content)


def main():
    """主函数(程序入口)"""
    with open('resources/news.csv', 'w') as file:
        writer = csv.writer(file)
        writer.writerow(('标题', '链接', '来源', '时间', '图片'))
        for page in range(1, 2):
            resp = requests.get(
                url='http://api.tianapi.com/topnews/index',
                params={
                    'key': '你自己申请的账号对应的Key',
                    'page': page,
                    'num': 30
                }
            )
            news_list = resp.json()['newslist']
            for news in news_list:
                picture_url = news['picUrl']
                if picture_url:
                    # filename = get_filename(picture_url)
                    filename = uuid.uuid1().hex + '.jpg'
                    download_picture(picture_url, f'resources/images/{filename}')
                else:
                    filename = ''
                writer.writerow((news['title'], news['url'], news['source'], news['ctime'], filename))


if __name__ == '__main__':
    main()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值