Read file and count repeated elements

Asked

Viewed 43 times

-3

Hey there, guys. I need some help solving a cucumber quickly. At work I have a file that has 18 characters per line (more than 600 lines). I need to read each line and in each line read 6 characters at a time. And in these 6 characters I want to count how many times it repeats in this file. Ex: ABCDEF - 4, ABDEDF - 1, AAAAAA - 10... I was researching that Python could be my solution (if someone has a silver bullet with other language is welcome). Sorry I don’t master programming to already leave something I started. But I saw that I can use something like this (Python):

from collections import Counter

with open('arq.txt') as f:
    ocorrencias = Counter(f.read().split())
print(ocorrencias) 
  • Have you tried using Slices strings? Research on this, maybe it will help you. :)

  • I would have to do a for getting the interactor and interactor + 6 as indexers?

  • https://repl.it/@hkotsubo/Frequentstimulatinglocatorprogram#main.py

1 answer

0


For future research of the type.

import json
import textwrap
from collections import Counter

group_size = 6
res = Counter()

with open('arq.txt') as f:
  for line in f:
    res.update({_: 1 for _ in textwrap.wrap(line, group_size)})

print(res)    
with open('saida.json', 'w') as f: #saída gravada em json
  json.dump(dict(res), f)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.