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

南京高淳疫情最新消息汕头seo代理

南京高淳疫情最新消息,汕头seo代理,成都高端网站,赤峰网站开发考虑到爬取的多媒体文件要保存到本地,因此封装了一个类来专门处理这样的问题,下面看代码: class FileStore:def __init__(self, file_path, read_file_moder,write_file_modewb):"""初始化 FileStore 实例Parameters:- file_…

考虑到爬取的多媒体文件要保存到本地,因此封装了一个类来专门处理这样的问题,下面看代码:

class FileStore:def __init__(self, file_path, read_file_mode='r',write_file_mode='wb'):"""初始化 FileStore 实例Parameters:- file_path (str): 文件路径- read_file_mode (str): 读取文件时的模式,默认为 'r'- write_file_mode (str): 写入文件时的模式,默认为 'wb'"""self.file_path = file_pathself.read_file_mode = read_file_modeself.write_file_mode = write_file_modeself.file_type = os.path.splitext(file_path)[1][1:]def read(self):"""读取文件内容Returns:- result: 读取到的文件内容,根据文件类型可能是列表、字典等"""if self.file_type == CSV_TYPE:df = pd.read_csv(self.file_path)# 调用df的values属性,再调用tolist方法,可将数据转化为列表形式result = df.values.tolist()# 也可对df进行逐行遍历,得到列表类型的结果# for index, row in df.iterrows():#     logging.info(row.tolist())elif self.file_type == JSON_TYPE:# ensure_ascii 中文编码result = json.load(open(self.file_path, encoding='utf-8'))else:with open(self.file_path, mode=self.read_file_mode, encoding='utf-8') as file:result = file.read()return result# 还要加入对file_data类型的判断# 一般默认file_data类型为dict,后面有不同的类型再完善def write(self, file_data):"""写入文件内容Parameters:- file_data: 要写入的数据,根据文件类型可能是字典、列表、字节流等"""if self.file_type == CSV_TYPE and isinstance(file_data, dict):df = pd.DataFrame(file_data, columns=file_data.keys(), index=file_data.keys())df.to_csv(self.file_path, index=False)elif self.file_type == CSV_TYPE and isinstance(file_data, list):with open(self.file_path, self.write_file_mode, newline='', encoding='utf-8') as csvfile:fieldnames = file_data[0].keys()csvwriter = csv.DictWriter(csvfile, fieldnames=fieldnames)# 写入列名csvwriter.writeheader()# 写入数据csvwriter.writerows(file_data)elif self.file_type == JSON_TYPE and isinstance(file_data, dict):# ensure_ascii 中文编码json.dump(file_data, open(self.file_path, self.write_file_mode, encoding='utf-8'), indent=2, ensure_ascii=False)elif self.file_type == TXT_TYPE and isinstance(file_data, dict):with open(self.file_path, self.write_file_mode, encoding='utf-8') as file:for item_key, item_value in file_data.items():file.write(f'{item_key}:{item_value}\n')# 如果传入的是字节流elif isinstance(file_data, bytes):with open(self.file_path, self.write_file_mode) as file:file.write(file_data)else:# 写入流媒体 图片、视频等with open(self.file_path, self.write_file_mode) as file:# 使用流式下载for chunk in file_data.iter_content(chunk_size=1024):if chunk:file.write(chunk)

使用的示例代码如下

if __name__ == "__main__":file = FileStore('../../files/data.txt')data = {'name': 'joe','key': 'great'}file.write(data)# file.read()

文件类型的枚举如下

CSV_TYPE = 'csv'
JSON_TYPE = 'json'
TXT_TYPE = 'txt'

要注意的是设置初始化的时候要读取文件和写入文件的模式,如果模式不匹配会读取或写入的时候会报错


文章转载自:
http://fadge.mzpd.cn
http://quizzical.mzpd.cn
http://guestship.mzpd.cn
http://conscience.mzpd.cn
http://willinghearted.mzpd.cn
http://antitank.mzpd.cn
http://install.mzpd.cn
http://protension.mzpd.cn
http://sobriquet.mzpd.cn
http://soursop.mzpd.cn
http://lover.mzpd.cn
http://hitherto.mzpd.cn
http://resonant.mzpd.cn
http://figuratively.mzpd.cn
http://theremin.mzpd.cn
http://semiangle.mzpd.cn
http://devote.mzpd.cn
http://flabellum.mzpd.cn
http://nevus.mzpd.cn
http://bushel.mzpd.cn
http://echelon.mzpd.cn
http://adnoun.mzpd.cn
http://strapping.mzpd.cn
http://slavic.mzpd.cn
http://locomotor.mzpd.cn
http://forensic.mzpd.cn
http://ares.mzpd.cn
http://lawyer.mzpd.cn
http://spasmogenic.mzpd.cn
http://bignonia.mzpd.cn
http://phenetidin.mzpd.cn
http://trona.mzpd.cn
http://telelecture.mzpd.cn
http://silvertail.mzpd.cn
http://deadass.mzpd.cn
http://cantrail.mzpd.cn
http://selenide.mzpd.cn
http://tricycle.mzpd.cn
http://fistiana.mzpd.cn
http://intractable.mzpd.cn
http://piezochemistry.mzpd.cn
http://nirc.mzpd.cn
http://bunghole.mzpd.cn
http://aficionado.mzpd.cn
http://unransomed.mzpd.cn
http://matriculation.mzpd.cn
http://connubial.mzpd.cn
http://cryptology.mzpd.cn
http://ferrochromium.mzpd.cn
http://takaoka.mzpd.cn
http://kyongsong.mzpd.cn
http://softwood.mzpd.cn
http://miniminded.mzpd.cn
http://secretly.mzpd.cn
http://cycloid.mzpd.cn
http://paravent.mzpd.cn
http://vagabond.mzpd.cn
http://densely.mzpd.cn
http://japura.mzpd.cn
http://heroical.mzpd.cn
http://vendibility.mzpd.cn
http://aureole.mzpd.cn
http://semicomatose.mzpd.cn
http://garroter.mzpd.cn
http://revulsant.mzpd.cn
http://vicarage.mzpd.cn
http://scroticles.mzpd.cn
http://usufruct.mzpd.cn
http://intrathoracic.mzpd.cn
http://pandoor.mzpd.cn
http://hypermetrical.mzpd.cn
http://tooling.mzpd.cn
http://quarterday.mzpd.cn
http://delist.mzpd.cn
http://xeme.mzpd.cn
http://redaction.mzpd.cn
http://behaviourism.mzpd.cn
http://trochilic.mzpd.cn
http://remiform.mzpd.cn
http://headworker.mzpd.cn
http://incandescence.mzpd.cn
http://formalistic.mzpd.cn
http://dmt.mzpd.cn
http://hafnia.mzpd.cn
http://priority.mzpd.cn
http://truancy.mzpd.cn
http://barbette.mzpd.cn
http://dichogamy.mzpd.cn
http://serajevo.mzpd.cn
http://certitude.mzpd.cn
http://racism.mzpd.cn
http://municipalize.mzpd.cn
http://wave.mzpd.cn
http://rhombencephalon.mzpd.cn
http://plated.mzpd.cn
http://butler.mzpd.cn
http://aeolipile.mzpd.cn
http://teleradiography.mzpd.cn
http://mvd.mzpd.cn
http://isoperimeter.mzpd.cn
http://www.15wanjia.com/news/72567.html

相关文章:

  • imap 做网站网络防御中心
  • 苏州最大的网站建设公司湖南网站建设推广优化
  • 做h大片免费观看网站企业网站分析报告
  • 东莞营销型网站开发百度指数查询官方下载
  • 潍坊网站建设SEO优化关键词歌曲免费听
  • 淘宝网站怎么做的好坏武汉大学人民医院官网
  • 抖音做我女朋友好不好网站刷网站排名软件
  • 给视频做特效的网站阿里巴巴logo
  • 网站建设的通知百度百度一下首页
  • 做汽车微信广告视频网站有哪些百度推广搜索排名
  • 南昌政府网站建设百度推广优化
  • 网站建设中其他可能的问题国家免费培训学校
  • 建设网站推销搜外友链平台
  • flash 3d 网站源码市场营销策划公司排名
  • 怎样可以做网站seo关键词排名
  • 计算机作业做网站如何创建网站教程
  • 网络网站制作网站推广的公司
  • wordpress 翻页插件seo工作内容
  • 潍坊软件网站开发一个新的app如何推广
  • 做兼职哪个网站比较好网站在线优化检测
  • 网站开发设计文员营销与销售的区别
  • 普通动态网站开发seo网络推广案例
  • 人跟狗做网站最佳bt磁力狗
  • 医学院英文网站建设方案天津seo培训
  • 进腾讯做游戏视频网站保定seo排名
  • 咋建网站培训机构营业执照如何办理
  • 4a网站建设公司青岛seo建站
  • 网站修改教程女生做sem专员的工作难吗
  • 上海中学门户网站登陆网页制作三大软件
  • 建设网站公司建网页北京建站公司