python实现文件编码格式转换

python实现⽂件编码格式转换
⽅法⼀
def _read(from_filename, from_encode):吉尔吉斯族
with open(from_filename, "r", encoding=from_encode) as f:
for lines in iter(lambda: tuple(islice(f, 1000000)), ()):
_queue.put(lines)
电子产品世界
_queue.put(None)
def convert_file_to_utf8(p_task, **kwargs):
"""码流分析仪
转化ftp下载的⽂件格式
长会口大桥:param p_task:  不同下载⽂件的task_id
:
param kwargs:
中世纪欧洲地图:return:
"""
local_file = kwargs['ti'].xcom_pull(task_ids=p_task)
convert_file = local_file[0:len(local_file) - 3] + "csv"
th = Process(target=_read, args=(local_file, "GB18030"))林木林地权属争议处理办法
th.start()
with open(convert_file, "w", encoding="utf-8") as f:
while True:
lines = _()
if lines is None:
break
f.write(''.join(lines))
th.join()
⽅法⼆
with open(filename, 'r', encoding="GB18030") as f:
i = 0
for row_ in f:
row = row_.strip().encode("utf-8").decode("utf-8")
i += 1
性能⽐较:对1M⼤⼩⽂件进⾏实验。⽅法⼀的消耗时间为44893微秒,⽅法⼆消耗时间为49015微秒。使⽤⽅法⼀⽐使⽤⽅法⼆转换⽂件格式更快。

本文发布于:2024-09-22 09:52:57,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/200428.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:格式   编码   转换   林地   电子产品   码流   大桥
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议