asking a Q and get it out of a csv file? - python

Q = question
I want to access information from a csv file, for example if the file included:
NAME|DATE OF BIRTH|PHONE NUMBER|
xxx|yyyyyyyyyyyyy| zzzzzzzzzzz|
xxx|yyyyyyyyyyyyy| zzzzzzzzzzz|
xxx|yyyyyyyyyyyyy| zzzzzzzzzzz|
How would I ask a question, example:
(pseudo code)
what is the name of the person you are looking for? :
And for python to get the information and display it?

Without writing all your code for you...
Break your problem down into the different components you need to implement:
Read a .CSV file
Organize the data for retrieval (one or more dicts, keyed by each field you might want to use to look up records, is probably the simplest solution)
Get user input ("ask a question")
Retrieve the requested data
Display the requested data to the user
Figure out (i.e. research) how to implement each of these individually, and then you can write your program to do that. If you then have a specific problem with a specific piece, come back to SO with the code you've written and explain the trouble you're having.

Related

saving data with lists and appending data to list as safety net, python

so what I've been trying to do is store data on a program so that way when I reopen my python program so all the previous data is still there. what I want to do is have the ability to use an input statement and allow the user to add data and then have it be saved. I want it to be saved if its numbers or words so if anyone has any suggestions or ideas to give me on how to do this please let me know or point me in the right direction please let me know.
list = [1, 2, 3, 4]
bon = str(next(reversed(list)))
print(bon)
a = input("new: ")
if a == 'new':
bag = bon + 1
You will need some time of database or storage. There are a few ways to do this.
You can create a CSV file (or a flat file with a custom delimiter). Each time your program is opened read from the CSV file and put the data in a list. Vice-versa you can just write new lines and save the file when new data is added. Although this is simple to begin with it can get even more complicated it you wish to do things like update data later on.
You can use a database. The easiest one to get started with is sqlite which creates a local DB file and doesn't need database software.
You can go further and use something like pickle but I can't see this being useful or the best use of your time in this case.
I would start with taking a look at :
Flat files for data storage
SQL to improve access to persistent data
SQLite for data storage
SQLAlchemy to work with data as Python objects
There's a pretty good article here https://realpython.com/python-sqlite-sqlalchemy/ that explains how to achieve that in Python.

Database searches in separate files

I am looking for a kind of database which can search in separate files eg. pdf, xls, doc that I get from different suppliers. My idea is something like this:
For example, I need to search for a part number and check different data about it. The file containing the part number must then be opened with the part number marked. If there are multiple hits, the database should display a list of the various files containing the searched item number. The list should act as links that open the file with the item number selected when selecting one from the list.
Does this already exist or how do I approach it?
Today, it's all assembled into a single PDF file of more than 1000 pages, and it's a time-consuming and laborious process to maintain.
I've only used vba in connection with Excel, so maybe it's too complicated for me. But is it possible for a programmer without spending 1000 hours on it?
Please help me :-)
Either Access or Excel could do this. I noticed the Python tag. I'm sure Python could handle this as well, although it seems more like a database solution would be best. It sounds like a one-to-many scenario. See the link below for some ideas of how this technique works.
https://www.tutorialspoint.com/ms_access/ms_access_one_to_many_relationship.htm
Also, below is a link with a whole bunch of MS Access templates. Take a look at that and hopefully that will give you some ideas of how to get started.
https://www.microsoftaccessexpert.com/Microsoft-Access-Templates.aspx
I agree, keeping this in a PDF with 1000 pages is NOT the way to go!!

Is there a way to save user input into a permanent list that is saved even after the python program is closed

I want to make a chatbot in python 3 that can learn on its own. I want it to be able to store data from the users input. How could I do this and keep it in a list so whenever i check back on it it will have new variables stored inside from different users.
Store the data in a file. Learn about reading and writing to files. Some code you could use it:
# Saving the information
file = open("filename.txt","w")
file.write('|'.join(my_list))
file.close()
# To get the information just do
file = open("filename.txt","r").read()
my_list = file.split("|")
# Magic you have your list back again!
There is a lot more to this so you should do some research about reading and writing to files.
https://www.guru99.com/reading-and-writing-files-in-python.html
For data to persist outside the program life-cycle you will need some form of permanent storage. Plain text files are an easy way to do this, as answered by Elodin. You can use JSON formatting to associate the data with each user, for example: [{"username":"John","input":"data entered by user 1"},{"username":"Mary","input":"data entered by user 2"}]. A better way, however, would be to use something like SQL/SQLite to store your data.

Opening NSFG data (from ThinkStats book) with Pandas

I'm going through the book ThinkStats. http://greenteapress.com/thinkstats/nsfg_data.html
I'd prefer to work with pandas because I'd like to strengthen my skill in that, but I'm having a hard time making out how to open this file.
http://greenteapress.com/thinkstats/nsfg_data.html
The usual pd.read_csv(filename) does not seem to work.
I'm also reading the code provided with the book, but it's a bit difficult to make out for me.
The pandas read_csv function will not work on this data set without some thinking about the data set itself. Indeed, it is neither a comma separated value nor a space separated format.
Instead, it is a kind of home-made format where the number of fields per line is not contant, which is another issue. Besides, number of spaces between values is not constant, which is another issue.
In order to better understand the format of the data files, I would recommend you get the code from the author. The link is provided in the book but it is here http://greenteapress.com/thinkstats/ and to play with the code provided to figure out the format being used
Provided you have the data file, you can use the survey module
import survey
preg = survey.Pregancies()
pre.ReadRecors(".")

Design help: Configurable behavior based on input

I am working in Python, but this is a general design question so general answers are welcome. I will explain the context not as part of the question, but to serve as an example:
I have a script which receives a CSV file, it uses the fields in this file to make automated phone calls. The fields represent names to be spoken, dates to be spoken, and the phone numbers to call. For example, input like "555-555-4321,Bob,Jill,3/30/2011" a phone call might be placed to 555-555-4321 and a robotic message delivered saying "Bob, don't forget Jills birthday is next Wednesday, one week from now."
My question is what design patterns would be useful for making this system configurable? More specifically, I'd like to specify what the format the input lines take, and some behaviors for generating the voice message. Some fields, like "Bob," can be as simple as "speak the field." Other fields, like the date, require some transformation in order to be spoken (ie, how does "3/30/2011" become "next Wednesday"). I'd also like to have various line formats, for example, input such as "555-555-4321,Bob,6:00" might call Bob every day at 6:00 and say "wake up!"
My goal is to have a web interface which allows defining and configuring these types of things. I know how to solve these problems by hacking my source code, but hacking the source code is a long ways from a simple and user friendly front-end.
I'm solving a related but not identical problem currently.
My solution is to create a control list of the same length as the target csv lines, where each element in the control list is the name of a useMethod. In my case the useMethod is an editor widget; in your case, it would be a function that defines how the field is interpreted by your text-to-speech engine. For each line, you can then iterate over the fields, calling the appropriate processing widget.
So for your example of "555-555-4321,Bob,Jill,3/30/2011",
import csv
def phoneNumber(number):
...
def userName(name):
...
def targetDate(datestring):
...
control = [phoneNumber, userName, userName, targetDate]
with open("csvFile", "r") as inFile:
reader = csv.reader(inFile)
for row in reader:
for op, item in zip(control, row):
op(item)
I note that this only works if the csv file has constant interpretation per element, but if it has variant interpretation then a csv file is the wrong storage method. I also note that you'd need some other control object to generate the rest of the sentence; this is left as an exercise for the reader. :)
This allows you to have a library of interpreter functions that can be assigned to fields in the csv file by simply changing the control string. A new control string would invoke a different order of field interpretations with no need to change the source code, and the new string could be entered on command line, stored in the first line of the csv file, or brought in some other way.
edit: And noting your addendum on using a web interface to configure, that would be a straight-forward way to provide a new control list.

Categories