dictionary - to check if a file belongs to a class in python -


i have 5 classes,

namely:

earn  acq  money  fx  crude 

i have list of 20000 files, , have document "topics.txt", of form:

earn~6~7~4  grain~9~1~2~12  money~4~29 

and on.. numbers correspond filename, , words correspond classes.

i need print files falling under classes have mentioned,i.e; "earn","acq","money","fx" , "crude"

ex output: (earn-6.txt,7.txt,4.txt)

(acq-5.txt)

and on..

i able print available classes in "topics.txt", want print 5 particular ones.

import collections import sys sys.stdout=open('dicti1.txt','w') open('topics.txt') f:     d = collections.defaultdict(list)     line in f:         value, *keys = line.strip().split('~')         key in filter(none, keys):             d[key].append(value+".txt")   in d.items():     print(i)     

unless i've misunderstood problem, working hard. suggest not overwriting sys.stdout.

try this:

interesting_types = ['earn', 'acq', 'money', 'fx', 'crude'] open("in.txt") in_file, open('out.txt', 'w') out_file:     l in in_file:         if l:             type, *filenames = l.strip().split("~")             if type in interesting_types:                 out_file.write("({}-{})\n".format(type, ",".join(["{}.txt".format(x) x in filenames]))) 

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -