Python怎么从文件里的数据去重复,去重复后,再保存到文件里,把重复的数据保存出来
Python怎么从文件里的数据去重复,去重复后,再保存到文件里,把重复的数据保存出来已下是文件里面的数据格式,如下"a","b","c""d","e""a","b","y...
Python怎么从文件里的数据去重复,去重复后,再保存到文件里,把重复的数据保存出来已下是文件里面的数据格式,如下
"a","b","c""d","e"
"a","b","y","hu","gy","j","等等"
(把文件里面全部的内容去重复) 展开
"a","b","c""d","e"
"a","b","y","hu","gy","j","等等"
(把文件里面全部的内容去重复) 展开
展开全部
内容读出来通过逗号分割成一个列表,再去掉重复的,再写入到文件里面
# -*- coding:utf-8 -*-
filename = 'data.txt'
with open(filename, 'r') as f:
content = f.read()
content = list(set(content.split(',')))
content = ','.join(content)
with open(filename, 'w') as _f:
_f.write(content)
展开全部
# a.txt
"a","b","c""d","e"
"a","b","y","hu","gy","j"
fn = 'a.txt'
with open(fn) as f:
s = f.read()
aa = set()
lines = []
for i in s.strip().split('\n'):
words = i.strip().split(',')
ws = []
for w in words:
if w not in aa:
aa.add(w)
ws.append(w)
if ws:
lines.append(','.join(ws))
with open(fn, 'w') as fp:
fp.write('\n'.join(lines))
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询