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

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")

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.

Print Lines from .csv based off criteria inputted from user

I am new to python and am looking for guidance on the task I am working on.
I am importing a csv file that looks like the list below.
invoice_id,customer_first_name,customer_last_name,part_number,quantity,total
18031,Hank,Scorpio,367,1,2.63
54886,Max,Power,171,3,50.79
19714,Jonathan,Frink,179,2,7.93
19714,Jonathan,Frink,378,2,32.34
22268,Gil,Gunderson,165,2,47.15
87681,Lionel,Hutz,218,1,50.83
84508,Lurleen,Lumpkin,257,1,81.95
34018,Lionel,Hutz,112,3,88.88
34018,Lionel,Hutz,386,3,86.04
34018,Lionel,Hutz,216,1,53.54
66648,Patty,Bouvier,203,3,70.47
I only want to print each line if its based off the criteria inputted by the user. For example, if the user inputs lname and then inputs Hutz the following would be printed out.
87681,Lionel,Hutz,218,1,50.83
34018,Lionel,Hutz,112,3,88.88
34018,Lionel,Hutz,386,3,86.04
34018,Lionel,Hutz,216,1,53.54
4 records found.
This is what I have so far...
salesdatafile= None
while True:
salesdatafilename=input("Enter the name of the file:")
try:
salesdata= open(salesdatafilename, 'r')
break
except FileNotFoundError:
print("{0} was not found".format ( salesdatafilename ))
search=input("Do you want to search by invoice id or lname? Please type id or lname: ")
idsearch=salesdata.readline()
if search== 'id':
idnumber=print(int(input('Please enter Id to search: ')))
while idsearch != '':
if idsearch== idnumber:
print(idsearch)
else:
lname=print(input('Please enter your last name: '))
while idsearch != '':
if idsearch== lname:
print(idsearch)
All that is printing out is lname or id inputted by the user.
Python has a csv module built in that you should be utilizing. Check out the below example code:
import csv
salesdatafilename = r"path/to/file.txt"
data = csv.reader(open(salesdatafilename))
last_name_to_look_for = "Lionel"
for invoice_id, customer_first_name, customer_last_name, part_number, quantity, total in data:
if customer_last_name == last_name_to_look_for:
print(invoice_id, customer_first_name, customer_last_name, part_number, quantity, total)
In your code you are comparing the entire line to the idnumber or lname. Instead you might want to try something like
if lname in idsearch:
<do something>
this will check if the lname is in the line somewhere.
For performance, I will use a generator to read the lines so that my program doesn’t crash were this to be a bigger file. So I will yield each line in that file with a generator function. Since generators return iteratirs, I will then iterate and filter every line that does not contain my search value and return only those that contain my search value.

Key found in dictionary that shouldn't exist

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?

The file in python (AttributeError: 'str' object has no attribute 'append')

`File = input("Please enter the name for your txt. file: ")
fileName = (File + ".txt")
WRITE = "w"
APPEND = "a"
file = []
name = " "
while name != "DONE" :
name = input("Please enter the guest name (Enter DONE if there is no more names) : ").upper()
fileName.append(name)
fileName.remove("DONE")
print("The guests list in alphabetical order, and it will save in " + fileName + " :")
file.sort()
for U in file :
print(U)
file = open(fileName, mode = WRITE)
file.write(name)
file.close()
print("file written successfully.")
`
I am just practicing to write the file in Python, but something bad happened. Please help me. Thank you.
The code.
The error description.
Here are still some errors about this :
fileName.remove("DONE")
Still showing 'str' error.
THANK YOU
You try to append to string which is not correct in Python, instead try:
filename += 'name'
You're trying to build a list of names. Start with a list:
guests = []
and then append the values provided by your user:
while name is not "Done":
prompt = "Please input the name of the next guest, or 'Done'."
guests.append(input(prompt).upper())
then you can sort that list and write the values to the file. (which you seem to have a handle on)
Appending the guests' names to fileName, or concatenating them onto it, wouldn't make a lot of sense. You'd end up with something like "data.txtJOEBOBJANELINDA" which would do you no good at all.

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

Categories

Resources