xml地图|网站地图|网站标签 [设为首页] [加入收藏]

Python实现的txt文件去重功能示例,python将文本分

来源:http://www.ccidsi.com 作者:最新解决方案 人气:119 发布时间:2019-12-16
摘要:python将文本分每两行黄金年代组并保存到文件,python两行 事情须求 亟需将文件文件分每两行朝气蓬勃组 jb51.txt 1:www.jb51.net 2:www.jb51.net 3:www.jb51.net 澳门皇家赌场娱乐 ,4:www.jb51.net 5:www

python将文本分每两行黄金年代组并保存到文件,python两行

事情须求

亟需将文件文件分每两行朝气蓬勃组

jb51.txt

1:www.jb51.net
2:www.jb51.net
3:www.jb51.net
澳门皇家赌场娱乐 ,4:www.jb51.net
5:www.jb51.net
6:www.jb51.net
7:www.jb51.net
8:www.jb51.net
9:www.jb51.net
10:www.jb51.net
11:www.jb51.net
12:www.jb51.net
13:www.jb51.net
14:www.jb51.net
15:www.jb51.net
16:www.jb51.net

主题代码:

# -*- coding: utf-8 -*-
'''
python读取文件,每两行为一组
'''
def fenhang(infile,outfile):

 infopen = open(infile,'r',encoding='utf-8')
 outopen = open(outfile,'w',encoding='utf-8')
 lines = infopen.readlines()
 i = 1
 for line in lines:
  if i % 2 == 0:
   outopen.write(line 'n')
  else:
   outopen.write(line)
  i  = 1
 infopen.close()
 outopen.close()
fenhang("jb51.txt","o.txt")

由此实行 python jb51.txt

o.txt的内容

澳门皇家赌场娱乐 1

到那边这篇文章就完了了,希望大家自此多多指教帮客之家。

业务须求 须求将文件文件分每两行生机勃勃组 jb51.txt 1:www.jb51.net 2:www.jb51.net 3:www.jb51.net 4:www....

事务供给

本文实例陈述了Python完成的txt文件去重效益。分享给大家供咱们参照他事他说加以考查,具体如下:

亟待将文件文件分每两行黄金年代组

# -*- coding:utf-8 -*-
#! python2
import shutil
a=0
readDir = "/Users/Administrator/Desktop/old.txt"  #old
writeDir = "/Users/Administrator/Desktop/new.txt" #new
# txtDir = "/home/Administrator/Desktop/1"
lines_seen = set()
outfile = open(writeDir, "w")
f = open(readDir, "r")
for line in f:
  if line not in lines_seen:
    a =1
    outfile.write(line)
    lines_seen.add(line)
    print(a)
    print('n')
outfile.close()
print("success")

jb51.txt

其中old.tx如下:

1:www.jb51.net
2:www.jb51.net
3:www.jb51.net
4:www.jb51.net
5:www.jb51.net
6:www.jb51.net
7:www.jb51.net
8:www.jb51.net
9:www.jb51.net
10:www.jb51.net
11:www.jb51.net
12:www.jb51.net
13:www.jb51.net
14:www.jb51.net
15:www.jb51.net
16:www.jb51.net

www.jb51.net
www.baidu.com
www.sina.com.cn
www.jb51.net
www.google.com
www.sohu.com
www.jb51.net
www.163.com

本文由68399皇家赌场发布于最新解决方案,转载请注明出处:Python实现的txt文件去重功能示例,python将文本分

关键词: 68399皇家赌场

上一篇:python计算机基础,编程基础

下一篇:没有了

最火资讯