Created
January 2, 2020 20:45
-
-
Save kchawla-pi/d96e5c066d26132484cf658b6b227e1c to your computer and use it in GitHub Desktop.
A piece of code to point out better coding practices
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import glob, hashlib, csv | |
# replace with file location | |
path = r'/mnt/chromeos/MyFiles/Downloads/CSVs/' | |
files = glob.glob(path + r"/*.csv") | |
output = r"\output.csv" | |
# this is where we'll store the values | |
hits = [] | |
# word we're searching for | |
firstLetter = 't' | |
wordLength = 5 | |
wordLength2 = 7 | |
# read the files | |
for file in files: | |
f = open(file, 'r') | |
# make a list of all the lines in the file | |
lines = [] | |
for line in f: | |
lines.append(line) | |
f.close() | |
# create a list of words within the file | |
wordlist = [] | |
for line in lines: | |
line = line.replace(',', '') | |
line = line.replace('\n', '') | |
wordlist.append(line.split(" ")) | |
# join the lines into one list | |
joinedlist = [] | |
for linelist in wordlist: | |
joinedlist += linelist | |
# search for the term within the joined list and add it to the hits list | |
for word in joinedlist: | |
if word.lower()[0] == firstLetter.lower(): | |
if len(word) == wordLength or len(word) == wordLength2: | |
hits.append([word, file, hashlib.md5(file.encode('utf-8')).hexdigest()]) | |
# remove duplicates from out hits list | |
hits = list(set(map(tuple, hits))) | |
# write the list to a csv file | |
file = open(path + output, 'w', newline='') | |
writer = csv.writer(file) | |
writer.writerows(hits) | |
file.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment