python去重复排序,按第一列排序文本文件,并重复计数python

I have a text file that needs to be sorted by the first column and merge all repeats with the count to the left of the data, and then write the sorted/counted data into an already created csv file.

Ex text file:

, 00.000.00.000, word, 00

, 00.000.00.001, word, 00

, 00.000.00.002, word, 00

, 00.000.00.000, word, 00

, 00.000.00.002, word, 00

, 00.000.00.000, word, 00

Ex result:

, 3, 00.000.00.000, word, 00

, 1, 00.000.00.001, word, 00

, 2, 00.000.00.002, word, 00

My code:

for ip in open("list.txt"):

with open(ip.strip()+".txt", "a") as ip_file:

for line in open("data.txt"):

new_line = line.split(" ")

if "blocked" in new_line:

if "src="+ip.strip() in new_line:

ip_file.write(", " + new_line[11])

ip_file.write(", " + new_line[12])

ip_file.write(", " + new_line[13])

for ip_file in os.listdir(sub_dir):

with open(os.path.join(sub_dir, ip_file), "a") as f:

data = f.readlines()

data.sort(key = lambda l: float(l.split()[0]), reverse = True)

Whenever I test the code, I get the error TypeError: 'str' object is not callable or something similar. I can't use .split() .read() .strip() etc without getting the error.

Question: How can I sort the files' contents and count repeating lines (without defining a function)?

I'm basically trying to:

sort -k1 | uniq -c | sed 's/^/,/' >> test.csv

解决方案D = {}

for k in open('data.txt'): #use dictionary to count and filter duplicate lines

if k in D:

D[k] += 1 #increase k by one if already seen.

else:

D[k] = 1 #initialize key with one if seen for first time.

for sk in sorted(D): #sort keys

print(',', D[sk], sk.rstrip(), file=open('test.csv', 'a')) #print a comma, followed by number of lines plus line.

#Output

, 3, 00.000.00.000, word, 00

, 1, 00.000.00.001, word, 00

, 2, 00.000.00.002, word, 00

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值