Matching Title Strings with Filenames - python-3.x

I'm trying to loop through a directory of image files and match the file's name to corresponding title string so that the path can be inserted into a database. What i have below works for about 90% of the files, but my string cleaning method is imperfect. Im looking for a smarter way of doing this. Thanks for your time.
filename example : "2020_Super_Baseball_usa.jpg"
title string example : "2020 Super Baseball"
def string_cleaner(string_one):
'''filter non alpha chars from filename'''
filter_by= ":*/?-_.,'!'"
new_string= ""
for char in filter_by:
for letter in string_one:
if letter == char:
if new_string == "":
new_string = string_one.replace(char, "")
else:
new_string = new_string.replace(char, "")
return new_string
def matcher(string):
'''matches title string to filename and returns full path'''
temp_path = ""
tempfilename = ""
path = "C:\\Users\\USER\\Desktop\\pyscripts\\web- scraping\\SNES_DB_Scrape\\snes\\"
for file in os.listdir(path):
tempfilename = string_cleaner(file[:-7]).lower().lstrip().rstrip()
if string.lower().lstrip() == tempfilename :
temp_path = os.path.join(path, file)
return temp_path
for name in nameList:
path = matcher(name)
print("Name is: " + name + '\n' + "path is : " + path)

considering your filename and title example, you can try below code:
filename = "2020_Super_Baseball_usa.jpg"
title = "2020 Super Baseball"
def string_cleaner(string_one):
'''filter non alpha chars from filename'''
new_string = ""
for char in string_one:
if char.isalnum(): #checking if string_one character if alphanumeric
new_string = new_string + char
elif char == '_':
new_string = new_string + " "
return new_string
temp = string_cleaner(filename[:-7])
#print(temp) #check output of string_cleaner function
if temp.lower().lstrip().rstrip() == title.lower().lstrip():
print("match")

Related

Python string formatting for encryption

I need help figuring out how to do these following thigns for a string
If the character is an uppercase: change it to lowercase with '!' sign on its both sides (e.g. 'M' --> '!m!');
If the character is a lowercase: change it to uppercase with '!' sign on its both sides (e.g. 'm' --> '!M!');
If the character is a digit: cube the digit, i.e. raise to the power of 3 (e.g. '5' --> '125');
If the character is a blank space: replace it with '$' symbol;
Otherwise: keep it as it is.
I don't know where to start, besides a possible loop statement.
You can do this:
string = "Mm5 #"
new_string = ""
for char in string:
if char.isupper():
new_string += "!" + char.lower() + "!"
elif char.islower():
new_string += "!" + char.upper() + "!"
elif char.isdigit():
new_string += str(int(char) ** 3)
elif char == " ":
new_string += "$"
else:
new_string += char
print(new_string)
>> !m!!M!125$#
Iterate over the string.
According to the transformation rules, transforms each character in the string.
Returns the new string.
def convertString(input):
output = ''
for char in input:
if char.isupper():
char = '!' + char.lower() + '!'
output += char
elif char.islower():
char = '!' + char.upper() + '!'
output += char
elif char.isdigit():
char = int(char) * int(char) * int(char)
char = str(char)
output += char
elif char.isspace():
char = '$'
output += char
else:
char = char
output += char
return output
if __name__ == '__main__':
input = 'Mm 5:)'
output = convertString(input)
print(output)
Output
!m!!M!$125:)
Here's answer for your question. It is in Kotlin but you can get the logic and convert it in Python.
fun main(){
println("Enter String: ")
val str = readln()
var newString = ""
for (s in str){
when{
s.isDigit() -> newString += (s.digitToInt() * s.digitToInt() * s.digitToInt())
s.isLowerCase() -> newString += "!" + s.uppercase() + "!"
s.isUpperCase() -> newString += "!" + s.lowercase() + "!"
s == ' ' -> newString += "$"
}
}
println("String formatted as: $newString")
}

How to remove numbers from strings in a list using one single for loop in python3?

Have this code where I'm trying to remove all the numerical characters from strings in file names. It seems everything going all right but file names don't change.
import os
files_list = os.listdir('/mnt/c/Users/maverick/Desktop/prank/prank')
for file_name in files_list:
count = 0
l = []
for char in file_name:
if not char.isdigit():
l.append(char)
new_string = ''.join(l)
print('New string: ' + new_string)
print('Old file name: ' + file_name)
file_name = new_string
print('New file name: ' + file_name)
count = count + 1
print(files_list)
If you want to rename you can use os.rename(). Also, to remove all numbers from the file names you can use maketrans on digits from string like this.
import os
from string import digits
path = '/mnt/c/Users/maverick/Desktop/prank/prank'
files_list = os.listdir(path)
new_files_list = []
remove_digits = str.maketrans('', '', digits)
for file_name in files_list:
count = 0
new_string = file_name.translate(remove_digits)
new_files_list.append(new_string)
print('New string: ' + new_string)
os.rename(os.path.join(path, file_name), os.path.join(path, new_string))
print('Old file name: ' + file_name)
file_name = new_string
print('New file name: ' + file_name)
count = count + 1
print('Old Filenames :', files_list)
print('New Filenames :', new_files_list)
You could acheieve that using list comprehensions:
files_list = os.listdir('/mnt/c/Users/maverick/Desktop/prank/prank')
processed = [''.join([c for c in name if not c.isdigit()]) for name in files_list]
Example:
files_list = ["abc", "abc1", "a2b3c4"]
processed = [''.join([c for c in name if not c.isdigit()]) for name in files_list]
print(processed) # >> ['abc', 'abc', 'abc']
Update
If your real purpose is to rename your files, you need the os.rename() method.
You need to rename the file itself, not just write the new string into old file name inside you script. You could use os.rename:
import os
path = '/mnt/c/Users/maverick/Desktop/prank/prank'
files_list = os.listdir(path)
for file_name in files_list:
count = 0
l = []
for char in file_name:
if not char.isdigit():
l.append(char)
new_string = ''.join(l)
print('New string: ' + new_string)
print('Old file name: ' + file_name)
os.rename(os.path.join(path, file_name), os.path.join(path, new_string))
print('New file name: ' + file_name)
count = count + 1
print(files_list)

Caesar Cipher shift on new lines

I am having a problem with my code trying to do an advanced caesar cipher shift. I changed a certain letter to another, and then added a string to certain parts of a file before encoding, but am having problems doing the shifting now. This is my code:
import string
import sys
count = 1
cont_cipher = "Y"
#User inputs text
while cont_cipher == "Y":
if count == 1:
file = str(input("Enter input file:" ""))
k = str(input("Enter shift amount: "))
purpose = str(input("Encode (E) or Decode (D) ?: "))
#Steps to encode the message, replace words/letter then shift
if purpose == "E":
my_file = open(file, "r")
file_contents = my_file.read()
#change all "e"s to "zw"s
for letter in file_contents:
if letter == "e":
file_contents = file_contents.replace(letter, "zw")
#add "hokie" to beginning, middle, and end of each line
lines = file_contents.split('\n')
def middle_message(lines, position, word_to_insert):
lines = lines[:position] + word_to_insert + lines[position:]
return lines
new_lines = ["hokie" + middle_message(lines[x], len(lines[x])//2, "hokie") + "hokie" for x in range(len(lines))]
#math to do the actual encryption
def caesar(word, shifts):
word_list = list(new_lines)
result = []
s = 0
for c in word_list:
next_ord = ord(c) + s + 2
if next_ord > 122:
next_ord = 97
result.append(chr(next_ord))
s = (s + 1) % shifts
return "".join(result)
if __name__ == "__main__":
print(caesar(my_file, 5))
#close file and add to count
my_file.close()
count = count + 1
The error I am getting is:
TypeError: ord() expected a character, but string of length 58 found
I know that I need to split it into individual characters, but am not sure how to do this. I need to use the updated message and not the original file in this step...

Siimple Python. Not sure why my program is outputting this

I am making a program to take in a sentence, convert each word to pig latin, and then spit it back out as a sentence. I have no idea where I have messed up. I input a sentence and run it and it says
built-in method lower of str object at 0x03547D40
s = input("Input an English sentence: ")
s = s[:-1]
string = s.lower
vStr = ("a","e","i","o","u")
def findFirstVowel(word):
for index in range(len(word)):
if word[index] in vStr:
return index
return -1
def translateWord():
if(vowel == -1) or (vowel == 0):
end = (word + "ay")
else:
end = (word[vowel:] + word[:vowel]+ "ay")
def pigLatinTranslator(string):
for word in string:
vowel = findFirstVowel(word)
translateWord(vowel)
return
print (string)
You have used the lower method incorrectly.
You should use it like this string = s.lower().
The parentheses change everything. When you don't use it, Python returns an object.
Built-in function should always use ()
Here is the corrected version of the code which should work:
s = input("Input an English sentence: \n").strip()
string = s.lower() #lowercasing
vStr = ("a","e","i","o","u")
def findFirstVowel(word):
for idx,chr in enumerate(word):
if chr in vStr:
return idx
return -1
def translateWord(vowel, word):
if(vowel == -1) or (vowel == 0):
end = (word + "ay")
else:
end = (word[vowel:] + word[:vowel]+ "ay")
def pigLatinTranslator(string):
for word in string:
vowel = findFirstVowel(word)
translateWord(vowel,word)
return
print(string)

Piglatin Conversion Program

I am trying to create a program that can convert both from and English sentence into Piglatin and from Piglatin into English.
So far, the English to Piglatin portions are working fine, but I am having trouble converting from Piglatin to English.
def eng2Pig(sentence):
sentsplit = sentence.split()
for part in sentsplit:
print((part[1:] + part[0] + "ay"), end = " ")
def pig2Eng(sentence):
sentv1 = sentence.replace("ay", "")
sentsplit = sentv1.split()
for part in sentsplit:
print(part[-1] + part[:-1], end = " ")
def aySearch(sentence):
numwords = len(sentence.split())
numay = sentence.count("ay ")
if numwords == numay:
pig2Eng(sentence)
else:
eng2Pig(sentence)
x = input("Enter your sentence: ")
x = x + " "
aySearch(x)
I am having troubles converting English words that originally contain ay. For example, today converted to Piglatin would be odaytay. However, I am replacing ay with "" to remove the extra added ay.
Perhaps I need to count the number of ay(s) in a word, then based off of that, decide if I want to remove more than one ay.
Thanks -
Good luck
One problem is doing a replace ("ay", "") will change the word say int s, so it will corrupt your sentence. Here's a better solution.
def pig2Eng(sentence):
eng = ""
sentsplit = sentence.split()
for word in sentsplit:
eng += word[-3:-2] + word[:-3] + " "
return eng
print (pig2Eng("igpay atinlay siay tupidsay"))
Also note that is usually better programming form to return the result rather than printing it in the function.
Here is what I ended up doing - this worked well.
def eng2Pig(sentence):
pig = ""
sentsplit = sentence.split()
for part in sentsplit:
pig += (part[1:] + part[0] + "ay" + " ")
return pig
def pig2Eng(sentence):
eng = ""
sentsplit = sentence.split()
for part in sentsplit:
eng += (part[-3] + part[:-3] + " ")
return eng
def aySearch(sentence):
numwords = len(sentence.split())
numay = sentence.count("ay ")
if numwords == numay:
return pig2Eng(sentence)
else:
return eng2Pig(sentence)
def main():
x = input("Enter your sentence: ")
x = x + " "
print(aySearch(x))
main()

Resources