Key found in dictionary that shouldn't exist - python

Hey having some issues here. I am reading a file with keys and translations, a typical line in the file is like so:
"welcome-screen" = "Welcome to the app!"
The file also has a language prefix stored in lang_key, e.g. lang_key = eng.
Currently I am trying to build a dict:
strings_dict = {}
f = open(os.path.join(root, file), 'r')
for line in f:
strings = line.split('"')[1::2]
if len(strings) == 2:
if strings[0] in strings_dict:
entry = strings_dict[strings[0]]
entry[lang_key] = strings[1]
strings_dict[strings[0]] = entry
print("modified entry")
else:
entry = {lang_key: strings[1]}
strings_dict[strings[0]] = entry
print("made entry")
The line.split pulls the words out of the quotation marks. e.g the line above becomes [welcome-screen, Welcome to the app!]. The key is the first word welcome-screen if it's already in the dictionary I want to pull its value out of the dictionary (the value is also a dictionary) and add an item for the key lang_key. If the key is not already in the dictionary I want to create a new entry. However when debugging it seems to print `modified entry" every single time. Not once does it create a new entry. Why is this happening?

Related

how to update a JSON file?

I have some code that stores data in a dictionary and than the dictionary is stored in a JSON file:
def store_data(user_inp):
list_of_letters = list(user_inp)
list_of_colons = []
nested_dict = {}
for letter in list_of_letters:
if letter == ':':
list_of_colons.append(letter)
jf = json.dumps(storage)
with open('myStorage.json', 'w') as f:
f.write(jf)
if len(list_of_colons) == 2:
str1 = ''.join(list_of_letters)
list2 = str1.split(':')
main_key = list2[0]
nested_key = list2[1]
value = list2[2]
if main_key not in storage:
storage[main_key] = nested_dict
nested_dict[nested_key] = value
print(storage, '\n', 'successfully saved!')
jf = json.dumps(storage)
with open('myStorage.json', 'w') as f:
f.write(jf)
elif main_key in storage:
if nested_key in storage[main_key]:
print('this item is already saved: \n', storage)
else:
storage[main_key][nested_key] = value
print(storage, '\n', 'successfully saved!')
jf = json.dumps(storage)
with open('myStorage.json', 'w') as f:
f.write(jf)
The problem is that every time I rerun the program and enter new data, the data in the JSON file is replaced by the data entered the last time I ran the program. For example: If I want to store this string: gmail:pass:1234. What my function does is this:
creates a dictionary with the user input and stores it in the JSON file:
{'gmail': {'pass': 1234}}
As long I don't close the program, the data I enter keeps adding to the JSON object. But if I close the program, run it again, and enter new data, the data I stored before is replaced by the data I entered last.
So what I want is that every time I enter a new piece of data to the dictionary, it will add it to the object stored in the JSON file. So if I run the program again and enter this input, gmail:pass2:2343, this is how it should be stored:
{'gmail': {'pass': '1234', 'pass2': '2343'}}
And if I enter this, zoom:id:1234567, I want it to add this to the object inside the JSON file, like so:
{'gmail': {'pass': '1234', 'pass2': '2343'} 'zoom': {'id': '1234567'}}
I really don't know how to fix this, I already researched but I can't find the solution to my specific case.
Hope you understand what I mean. Thank you in advance for your help.
I think this is what you are trying to do:
def update_with_item(old, new_item):
changed = True
top_key, nested_key, value = new_item
if top_key in old:
if nested_key in old[top_key]:
changed = False
print("This item is already saved: \n", storage)
else:
old[top_key][nested_key] = value
else:
old[top_key] = {nested_key: value}
return old, changed
def main():
stored = json.load(open('myStorage.json'))
old, changed = update_with_item(stored, list2)
if changed:
jf = json.dumps(old)
with open('myStorage.json', 'w') as f:
f.write(jf)
print(storage, '\n', 'successfully saved!')
I'm also not sure how you looping over the code in main, or where the list2 variable is coming from. The main function here will need to be updated to how you are looping over creating the new values etc.
The update_with_item function should resolve the issue you are having with updating the dictionary though.

Problems with trying to call a dictionary using an input between files

I am creating a dictionary which is added into a separate file. I am trying to use an input (dictSearch) to try and return the dictionary and its contents like it is shown in the other file (file.py). The problem is that the program is searching through the second file for a string called "dictSearch", not for what dictSearch is assigned to.
I have tried to change the data type of dictSearch.
def searchFunction():
with open('file.py') as myfile:
dictSearch = input("Enter name of donor (MAKE NAME ONE WORD WITH FIRST LETTER CAPS! ex. \"SomeText\"): ")
if dictSearch in myfile.read():
print(dictSearch)
else:
print("False")
#Below is what file.py looks like.
Andy = {
'age':'18',
'bloodType':'O-',
'diseases':'NA',
'medications':'NA',
'name':'Andy',
}
MATT = {
'age':'21',
'bloodType':'A',
'diseases':'NA',
'medications':'NA',
'name':'MATT',
}
Ex2 = {
'age':'1354',
'bloodType':'B',
'diseases':'NA',
'medications':'NA',
'name':'Ex2',
}
#Below is how I am creating these unique dictionaries in a separate file (in file.py)
with open('file.py','a') as file: #Code from lines x-y gotten from https://stackoverflow.com/questions/36965507/writing-a-dictionary-to-a-text-file
file.write(str(dictName) + " = { \n")
for k in sorted (donorDictionary.keys()):
file.write("'%s':'%s', \n" % (k, donorDictionary[k]))
file.write("} ")
file.write("\n")
file.close()
I expect the output to return the input dictionary and its key/value pairs, but it is returning the string that dictSearch is assigned to.
You can just import the dictionary you defined in file.py, so if the file.py looks like
file.py
dct = {....}
Then you code will change to
#Import dct from file
from file import dct
def searchFunction():
dictSearch = input("Enter name of donor (MAKE NAME ONE WORD WITH FIRST LETTER CAPS! ex. \"SomeText\"): ")
#Perform your search
if dictSearch in dct:
print(dictSearch)
else:
print("False")

Python: How to compare text in user input with content in text file or mysql database?

If I add these 2 variables in my python app, how do I compare input entered into 'key' with line of texts (integer e.g: 12345, 67890) for finding matching value in a text file located at www.example.com/key.txt ?
name = raw_input("What is your name ? ")
key = raw_input("What is your key ? ")
I've also thought about comparing it with database content, but below code don't work.
import mysql.connector
email = raw_input("What is your email ? ")
key = raw_input("What is your key ? ")
cnx = mysql.connector.connect(user='root', password="79032", database='license_key')
cursor = cnx.cursor()
query = ("SELECT email_address, license_key FROM validation "
"WHERE license_key LIKE key")
cursor.execute(query, (license_key))
for (email_address, license_key) in cursor:
print("you license key is valid"))
cursor.close()
cnx.close()
You would need to read in the text file (a dictionary would work), then check for the key / assign a new value that way. You could then save it to text again, if desired.
Example: If you had a text file with two columns (1st being the key and 2nd the value), you could do the following:
example_dict = {}
# Open the file (this takes a path to the file)
with open("key.txt") as f:
# Loop over each line
for line in f:
# Split each line and get the 1st (key) and 2nd (val) values
(key, val) = line.split()
# Cast the key to an integer (not necessary if your key is a string)
example_dict[int(key)] = val
Then, look for the key:
name = raw_input("What is your name ? ")
key = raw_input("What is your key ? ")
# Here you check if the key is present
if key in example_dict:
# Here you set it to something new, like the name you got, if you wanted to
example_dict[key] = name

Multiple Word Search not Working Correctly (Python)

I am working on a project that requires me to be able to search for multiple keywords in a file. For example, if I had a file with 100 occurrences of the word "Tomato", 500 for the word "Bread", and 20 for "Pickle", I would want to be able to search the file for "Tomato" and "Bread" and get the number of times it occurs in the file. I was able to find people with the same issue/question, but for other languages on this site.
I a working program that allows me to search for the column name and tally how many times something shows up in that column, but I want to make something a bit more precise. Here is my code:
def start():
location = raw_input("What is the folder containing the data you like processed located? ")
#location = "C:/Code/Samples/Dates/2015-06-07/Large-Scale Data Parsing/Data Files"
if os.path.exists(location) == True: #Tests to see if user entered a valid path
file_extension = raw_input("What is the file type (.txt for example)? ")
search_for(location,file_extension)
else:
print "I'm sorry, but the file location you have entered does not exist. Please try again."
start()
def search_for(location,file_extension):
querylist = []
n = 5
while n == 5:
search_query = raw_input("What would you like to search for in each file? Use'Done' to indicate that you have finished your request. ")
#list = ["CD90-N5722-15C", "CD90-NB810-4C", "CP90-N2475-8", "CD90-VN530-22B"]
if search_query == "Done":
print "Your queries are:",querylist
print ""
content = os.listdir(location)
run(content,file_extension,location,querylist)
n = 0
else:
querylist.append(search_query)
continue
def run(content,file_extension,location,querylist):
for item in content:
if item.endswith(file_extension):
search(location,item,querylist)
quit()
def search(location,item,querylist):
with open(os.path.join(location,item), 'r') as f:
countlist = []
for search in querylist: #any search value after the first one is incorrectly reporting "0"
countsearch = 0
for line in f:
if search in line:
countsearch = countsearch + 1
countlist.append(search)
countlist.append(countsearch) #mechanism to update countsearch is not working for any value after the first
print item, countlist
start()
If I use that code, the last part (def search) is not working correctly. Any time I put a search in, any search after the first one I enter in returns "0", despite there being up to 500,000 occurrences of the search word in a file.
I was also wondering, since I have to index 5 files with 1,000,000 lines each, if there was a way I could write either an additional function or something to count how many times "Lettuce" occurs over all the files.
I cannot post the files here due to their size and content. Any help would be greatly appreciated.
Edit
I also have this piece of code here. If I use this, I get the correct count of each, but it would be much better to have a user be able to enter as many searches as they want:
def check_start():
#location = raw_input("What is the folder containing the data you like processed located? ")
location = "C:/Code/Samples/Dates/2015-06-07/Large-Scale Data Parsing/Data Files"
content = os.listdir(location)
for item in content:
if item.endswith("processed"):
countcol1 = 0
countcol2 = 0
countcol3 = 0
countcol4 = 0
#print os.path.join(currentdir,item)
with open(os.path.join(location,item), 'r') as f:
for line in f:
if "CD90-N5722-15C" in line:
countcol1 = countcol1 + 1
if "CD90-NB810-4C" in line:
countcol2 = countcol2 + 1
if "CP90-N2475-8" in line:
countcol3 = countcol3 + 1
if "CD90-VN530-22B" in line:
countcol4 = countcol4 + 1
print item, "CD90-N5722-15C", countcol1, "CD90-NB810-4C", countcol2, "CP90-N2475-8", countcol3, "CD90-VN530-22B", countcol4
You are trying to iterate over your file more than once. After the first time, the file pointer is at the end so subsequent searches will fail because there's nothing left to read.
If you add the line:
f.seek(0), this will reset the pointer before every read:
def search(location,item,querylist):
with open(os.path.join(location,item), 'r') as f:
countlist = []
for search in querylist: #any search value after the first one is incorrectly reporting "0"
countsearch = 0
for line in f:
if search in line:
countsearch = countsearch + 1
countlist.append(search)
countlist.append(countsearch) #mechanism to update countsearch is not working for any value after the first
f.seek(0)
print item, countlist
PS. I've guessed at the indentation... You really shouldn't use tabs.
I'm not sure I get your question completely, but how about something like this?
def check_start():
raw_search_terms = raw_input('Enter search terms seperated by a comma:')
search_term_list = raw_search_terms.split(',')
#location = raw_input("What is the folder containing the data you like processed located? ")
location = "C:/Code/Samples/Dates/2015-06-07/Large-Scale Data Parsing/Data Files"
content = os.listdir(location)
for item in content:
if item.endswith("processed"):
# create a dictionary of search terms with their counts (initialized to 0)
search_term_count_dict = dict(zip(search_term_list, [0 for s in search_term_list]))
for line in f:
for s in search_term_list:
if s in line:
search_term_count_dict[s] += 1
print item
for key, value in search_term_count_dict.iteritems() :
print key, value

Cannot add new items into python dictionary

Hi I'm new to python. I am trying to add different key value pairs to a dictionary depending on different if statements like the following:
def getContent(file)
for line in file:
content = {}
if line.startswith(titlestart):
line = line.replace(titlestart, "")
line = line.replace("]]></title>", "")
content["title"] = line
elif line.startswith(linkstart):
line = line.replace(linkstart, "")
line = line.replace("]]>", "")
content["link"] = line
elif line.startswith(pubstart):
line = line.replace(pubstart, "")
line = line.replace("</pubdate>", "")
content["pubdate"] = line
return content
print getContent(list)
However, this always returns the empty dictionary {}.
I thought it was variable scope issue at first but that doesn't seem to be it. I feel like this is a very simple question but I'm not sure what to google to find the answer.
Any help would be appreciated.
You reinitialize content for every line, move the initialization outside of the loop:
def getContent(file)
content = {}
for line in file:
etc.

Categories

Resources