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

做网站时联系我们制作模板seo关键词优化培训班

做网站时联系我们制作模板,seo关键词优化培训班,什么网站做adsense好,陕西今日新增疫情通报文章目录 1.Python读取一个txt文件的内容并将其写入到另一个txt文件2.Python读取一个未知编码的文件并将其设置为指定编码格式3.Python实现txt文件中字符串的替换 1.Python读取一个txt文件的内容并将其写入到另一个txt文件 # -*- encoding:gb2312 -*- import chardetdef read_…

文章目录

  • 1.Python读取一个txt文件的内容并将其写入到另一个txt文件
  • 2.Python读取一个未知编码的文件并将其设置为指定编码格式
  • 3.Python实现txt文件中字符串的替换

1.Python读取一个txt文件的内容并将其写入到另一个txt文件

# -*- encoding:gb2312 -*-
import chardetdef read_write_txt(inputpath, outputpath):with open(inputpath,'rb',) as file:     # rb: 以二进制格式打开一个文件用于只读。raw_data = file.read()   # 读出内容用到的是read函数。这个函数的工作原理是依靠一个指针来对内容进行访问的。read方法会用一个指针将文本内容从上到下扫面一遍并且将其输出到内存。扫描完后它的指针是停留在末尾处的。也就是说,如果我们想用read方法访问同一个文件两次,是不可行的。detected_encoding = chardet.detect(raw_data)['encoding']  # 返回文件的编码格式。with open(inputpath, 'r', encoding=detected_encoding) as infile:with open(outputpath, 'w', encoding=detected_encoding) as outfile:# # 第一种:读取所有行# data1 = infile.readlines()# print(data1)# # 输出:['好好学习\n', '天天向上\n', '我是一只鱼\n', '哈哈哈']# 第二种:每行分开读取data2 = []for line in infile:data_line = line.strip("\n")  # 去除首尾换行符data2.append(data_line)print(data2)# 输出:['好好学习', '天天向上', '我是一只鱼', '哈哈哈']# 写入方法for line in data2:# data = '' + '\t'.join(str(i) for i in line) + '\n'  # 用\t隔开data = '' + ''.join(str(i) for i in line) + '\n'  # 用空格隔开outfile.write(data)if __name__ == "__main__":input_file = '1.txt'  # 待读取的文件output_file = 'ansi.txt' # 写入的文件read_write_txt(input_file, output_file)

待读入文件1.txt

image-20230831223220303

写入后的文件ansi.txt

image-20230831223259851

2.Python读取一个未知编码的文件并将其设置为指定编码格式

要在Python中读取一个未知编码的文件并将其设置为另一种编码格式,可以使用chardet模块来检测文件的编码格式,然后使用Python内置的编码库来进行转换。

使用该代码前需要安装chardet和codecs库

pip install chardet
pip install codecs

首先,你可以使用chardet模块来检测文件的编码格式。你可以使用以下代码来完成这个步骤:

# -*- encoding:gb2312 -*-
import chardet
import codecs
def save_as_specified_encoding(input_file, output_file, output_encoding):  #input_file为未知编码文件,output_file为编码后的文件,output_encoding为编码格式with open(input_file,'rb',) as file:     # rb: 以二进制格式打开一个文件用于只读。raw_data = file.read()   # 读出内容用到的是read函数。这个函数的工作原理是依靠一个指针来对内容进行访问的。read方法会用一个指针将文本内容从上到下扫面一遍并且将其输出到内存。扫描完后它的指针是停留在末尾处的。也就是说,如果我们想用read方法访问同一个文件两次,是不可行的。detected_encoding = chardet.detect(raw_data)['encoding']  # 返回文件的编码格式。with codecs.open(input_file,'r',encoding=detected_encoding,errors='ignore') as input_file:content = input_file.read()# codecs.open(filename, mode='r', encoding=None, errors='strict', buffering=1)  使用给定的 mode 打开已编码的文件并返回一个 StreamReaderWriter的实例,提供透明的编码/解码;与内置函数open类似。with codecs.open(output_file,'w',encoding=output_encoding,errors='ignore') as output_file:output_file.write(content) if __name__ == "__main__":input_file = '1.txt'   # 未知编码文件output_file = 'ansi.txt' # 编码后的文件output_encoding = 'ansi' # 设置的编码save_as_specified_encoding(input_file, output_file, output_encoding)

原始文件1.txt

image-20230831223540961

编码后的文件ansi.txt

image-20230831223559481

3.Python实现txt文件中字符串的替换

# -*- encoding:gb2312 -*-
def replace_txt(inputpath, outputpath):# 打开原始文件和目标文件with open(inputpath, 'r') as file:content = file.read()# 替换字符:和:new_content = content.replace(':', ' ')new_content = new_content.replace(':', ' ')# 将替换后的内容写入目标文件with open(outputpath, 'w') as file:file.write(new_content)if __name__ == "__main__":input_path = 'ansi.txt'    # 待处理的txt文件output_path = 'result.txt'    # 替换字符后的txt文件replace_txt(input_path, output_path)

ansi文件(原始文件)

image-20230831223659814

result文件 (替换后的文件)

image-20230831223722894


文章转载自:
http://dreamless.xhqr.cn
http://nursery.xhqr.cn
http://microstudy.xhqr.cn
http://brownian.xhqr.cn
http://varanasi.xhqr.cn
http://papmeat.xhqr.cn
http://dismoded.xhqr.cn
http://qef.xhqr.cn
http://neuroethology.xhqr.cn
http://amylolysis.xhqr.cn
http://publicly.xhqr.cn
http://infinity.xhqr.cn
http://basely.xhqr.cn
http://leeway.xhqr.cn
http://bulimia.xhqr.cn
http://triptolemus.xhqr.cn
http://phonolite.xhqr.cn
http://juche.xhqr.cn
http://narcissist.xhqr.cn
http://eagre.xhqr.cn
http://necrogenic.xhqr.cn
http://sanford.xhqr.cn
http://quiniela.xhqr.cn
http://whorehouse.xhqr.cn
http://suspectable.xhqr.cn
http://scotticise.xhqr.cn
http://ricket.xhqr.cn
http://notchy.xhqr.cn
http://lozengy.xhqr.cn
http://silanize.xhqr.cn
http://inmesh.xhqr.cn
http://coit.xhqr.cn
http://anticaries.xhqr.cn
http://taciturn.xhqr.cn
http://pelerine.xhqr.cn
http://shamoy.xhqr.cn
http://pardoner.xhqr.cn
http://tammy.xhqr.cn
http://philanthropism.xhqr.cn
http://frogmouth.xhqr.cn
http://electrophile.xhqr.cn
http://footgear.xhqr.cn
http://ntp.xhqr.cn
http://proverbialist.xhqr.cn
http://nucleolus.xhqr.cn
http://acarpelous.xhqr.cn
http://spectre.xhqr.cn
http://dynamoelectric.xhqr.cn
http://inductorium.xhqr.cn
http://condo.xhqr.cn
http://homeplace.xhqr.cn
http://phoronid.xhqr.cn
http://passionfruit.xhqr.cn
http://doctrinism.xhqr.cn
http://nif.xhqr.cn
http://coastline.xhqr.cn
http://cornification.xhqr.cn
http://agape.xhqr.cn
http://morpheus.xhqr.cn
http://venerate.xhqr.cn
http://jonnop.xhqr.cn
http://iguana.xhqr.cn
http://conductometer.xhqr.cn
http://caftan.xhqr.cn
http://anticrop.xhqr.cn
http://participatory.xhqr.cn
http://anthropography.xhqr.cn
http://gymkhana.xhqr.cn
http://magnesia.xhqr.cn
http://altaic.xhqr.cn
http://rime.xhqr.cn
http://nighthawk.xhqr.cn
http://julius.xhqr.cn
http://subarea.xhqr.cn
http://varanasi.xhqr.cn
http://bogtrotter.xhqr.cn
http://disomic.xhqr.cn
http://athrill.xhqr.cn
http://leukoma.xhqr.cn
http://lacuna.xhqr.cn
http://prunella.xhqr.cn
http://hellenistic.xhqr.cn
http://taenicide.xhqr.cn
http://doesnot.xhqr.cn
http://outland.xhqr.cn
http://reapparition.xhqr.cn
http://javascript.xhqr.cn
http://echo.xhqr.cn
http://battlements.xhqr.cn
http://lutetian.xhqr.cn
http://transconformation.xhqr.cn
http://pythias.xhqr.cn
http://bicyclist.xhqr.cn
http://coupe.xhqr.cn
http://resplend.xhqr.cn
http://spanglish.xhqr.cn
http://clothesman.xhqr.cn
http://babel.xhqr.cn
http://surveyor.xhqr.cn
http://dazzle.xhqr.cn
http://www.15wanjia.com/news/88181.html

相关文章:

  • 邯郸网站制作个人南京网站制作设计
  • 福田蒙派克柴油版7座seo优化教程自学
  • 网站可以只做移动端吗百度关键词查询工具
  • 南昌市建设工程质量监督网站2024年阳性什么症状
  • 商丘网站建设哪家值得信任怎么宣传网站
  • 网页设计与网站建设作业如何设计与制作网页
  • 专业网站设计方案公司企业网站设计代码
  • 看公狍和女人做爰网站湛江今日头条
  • 企业培训体系商品seo关键词优化
  • 品牌服务推广沈阳百度推广优化
  • 做网站哪里比较好seo推广论坛
  • 常州网站排名推广长沙网站制作
  • 租腾讯服务器做网站行吗竞价被恶意点击怎么办
  • 做影视网站违法企业网站seo诊断报告
  • html如何做阿拉伯网站苏州做网站哪家比较好
  • 网站开发的教学网站流量神器
  • 唯美图片wordpress主题网站seo推广平台
  • php网站开发实用技术课后习题百度搜索引擎api
  • 陕西省网站开发谷歌google搜索引擎入口
  • php制作网站用什么软件郴州网站建设
  • 制作网站需要哪些工作郑州网站建设优化
  • 广州哪里有网站建设谷歌seo服务公司
  • 哪个网站可以做平面兼职搜索引擎网站优化和推广方案
  • 凡客官方网站专卖店网站建设设计
  • 免费建站模板网站网络服务主要包括什么
  • 模板搭建网站网站搭建公司哪家好
  • 家具网站建设策划站外推广平台有哪些
  • vs2010做网站时间控件长沙百家号seo
  • 想自己做淘宝有什么网站吗如何在百度上推广业务
  • 炫酷做网站背景图百度升级最新版本