read and display data from text file from database query in Python - python

I am newbie in python and i am stuck with kind of database engine problem in python
I have text file database table(person.text) with a delimiter(|) separated in python. For example
sue|45|Happy Lane|456-3245
John|43|67 Drury Lane|897-3456
Mark|21|Stuffy Street|345-7896
Now i want a functionality that take queries from user and fetch data from this text file and display. queries can be select,update (with or without where clause).
For example If user give input as "select name from person " then output would be
sue
John
Mark
I am confuse which data structure should i use ??

Instead of data = f.split("|") it should be data = line.split("|").
I need to write query in user input and then it must be interpreted by code. If i use above solution then how can a match my SQL queries with dictionary ?
Thanks

If you want to maintain your current text based database you would probably need to parse it manually then store it do a local dictionary (defaultdict makes it easy) to allow for keyword searching. I added a numeric primary key to help store the data in a keyword-searchable form
from collections import defaultdict
person = defaultdict(lambda: dict())
with open("path/to/person.txt", "r") as f:
primary_key = 0
for line in f:
data = line.split("|")
person[primary_key]["name"] = data[0]
person[primary_key]["age"] = data[1]
person[primary_key]["address"] = data[2]
person[primary_key]["pnum"] = data[3]
primary_key += 1
Then you would have a local dictionary named person that can be searched through using keywords inputted by the end user.
Searching:
query = "select name from person"
query_items = query.split(" ")
if query_items[0] == "select":
table = eval(query_items[3])
for value in table.values():
print(value[query_items[1]])

Related

Populating Insert Statements from a local file

I am trying to write user data from a file into a series of insert statements. I feel I am close but just missing one or two things. I am attempting to run a .format, but all I end up with are ?'s
import time, json, sqlite3
def insertsfromfile(file):
results = open(file).readlines()
output = open('UserINSERTFile.txt', 'w')
for rows in results:
jsonobject = json.loads(rows)
userid = jsonobject['user']['id']
name = jsonobject['user']['name']
screenname = jsonobject['user']['screen_name']
description = jsonobject['user']['description']
friendscount = jsonobject['user']['friends_count']
insert = ('INSERT INTO Users VALUES (?,?,?,?,?'.format(userid, name, screenname,description, friendscount)
insert = insert[:-1] + ''
output.write(insert)
output.close()
Thanks
I figured it out after reviewing it. Essentially I was missing that I had to combine the attributes together with my Insert string with the '+'. Also had to convert the variables to str() in case they were int.

Using a 'one key to many values' entry in SQLite3 database with Python

I have a text file that contains many different entries. What I'd like to do is take the first column, use each unique value as a key, and then store the second column as values. I actually have this working, sort of, but I'm looking for a better way to do this. Here is my example file:
account_check:"login/auth/broken"
adobe_air_installed:kb_base+"/"+app_name+"/Path"
adobe_air_installed:kb_base+"/"+app_name+"/Version"
adobe_audition_installed:'SMB/Adobe_Audition/'+version+'/Path'
adobe_audition_installed:'SMB/Adobe_Audition/'+version+'/ExePath'
Here is the code I'm using to parse my text file:
val_dict = {}
for row in creader:
try:
value = val_dict[row[0]]
value += row[1] + ", "
except KeyError:
value = row[1] + ", "
val_dict[row[0]] = value
for row in val_dict.items():
values = row[1][:-1],row[0]
cursor.execute("UPDATE 'plugins' SET 'sets_kb_item'= ? WHERE filename= ?", values)
And here is the code I use to query + format the data currently:
def kb_item(query):
db = get_db()
cur = db.execute("select * from plugins where sets_kb_item like ?", (query,))
plugins = cur.fetchall()
for item in plugins:
for i in item['sets_kb_item'].split(','):
print i.strip()
Here is the output:
kb_base+"/Installed"
kb_base+"/Path"
kb_base+"/Version"
It took me many tries but I finally got the output the way I wanted it, however I'm looking for critique. Is there a better way to do this? Could my entire for item in plugins.... print i.strip() be done in one line and saved as a variable? I am very new to working with databases, and my python skills could also use refreshing.
NOTE I'm using csvreader in this code because I originally had a .csv file - however I found it was just as easy to use the .txt file I was provided.

How do I change values in a dictionary in one key?

For example, I have a key = name. Within name, there's birthday, age, and phone number. I would only like to change say birthday but keep the rest. And I'm trying to use an existing file that have the names already.
So if you want to use a tuple as a key the syntax would be
d[(name, phoneNumber)] = birthday
as far as using values in a pre-existing file you will need the open method.
file = open("fileName.csv",r) # assuming that you have a file of comma separated values
text = file.read()
.split("\n")
peps = [p.split(",") for p in text]
dictionary = {}
for p in peps:
d[(p[column of name] , p[col# for phone number)]]=p[#Bday]
Roughly. There are several types of collections in python, {}, [], (), hash and set. I would recommend that you read up on each here

MongoDB field value to variable in python

I am taking entries from MongoDB and I want to do some modifications, data crunching etc and updating. In this particular example Iam trying for every document in collection
{u'time': 1405694995.310651, u'text': u'HOHO,r\u012bt ar evitu uz positivus ar vip bi\u013ceti kabat\u0101:)', u'_id': ObjectId('53cd621d51f4fbe9f6e04da4'), u'name': u'Madara B\u013cas\u0101ne', u'screenName': u'miumiumadara'} take its text value as a string, count its keyword values and after add to exact particular document field with keyword value.
I am struggling with taking text field as string so it can be operated. And also I havent found solution in python how to add new field to document with count variable. In a Mongo shell comands are easy, but here i dont know. Anything for me to look for?
db = conn.posit2014
collection = db.ceturtdiena
cursor = db.all.find()
for text_fromDB in cursor:
print text_fromDB
source_text = text_fromDB.translate(None, '#!#£$%^&*()_:""?><.,/\|+-')
source_text = source_text.lower()
source_words = source_text.split()
count = 0
word_list = []
with open('pozit.txt') as inputfile:
for line in inputfile:
word_list.append(line.strip())
for word in word_list:
if word in source_words:
count += 1
#add count variable to each document
# {$set : {value:'count'}}
AFAIK text_fromDB is just a dict so you can do this. (If you mean to update document)
text_fromDB['count'] = value
collection.update({'_id':text_fromDB['_id']}, {"$set": text_fromDB})
I'm not sure if I understand everything you're ask. Let's go one piece at a time. To get the text field from your collection as a normal string try this:
collection = db.centurtdiena
for doc in collection.find():
text = str(doc['text'])
print(text)

Search for a variable in a file and get its value with python

I want to have some variables that are stored in a file (text file or yaml file)
for example if I have these variables stored in the file
employee = ['Tom', 'Bob','Anny']
salary = 200
managers = ['Saly','Alice']
and I want the user to enter the list name or the variable name for example
if the user entered employee and want to do some operations on the list values so the user supposed to access employee[0], employee[1] .... etc
how can I write a python script that will go to the file search for the correct variable and give the user access to its value
Thanks
Like what #Levon said, there are several ways that allow you do that, and the best depends on your problem context. for example, you could
read the file yourself by formatting it e.g., via delimiter "=" in your file
use a database to store your data
use pickle or shelve to serialize your variables and get them back later.
put the variables in a python module and import it
This approach might be one way assuming your file contents is somewhat consistent:
Updated: I added the code necessary to parse the lists which previously wasn't provided.
The code takes all of the data in your file and assigns it to the variables as appropriate types (i.e., float and lists). The list parsing isn't particularly pretty, but it is functional.
import re
with open('data.txt') as inf:
salary = 0
for line in inf:
line = line.split('=')
line[0] = line[0].strip()
if line[0] == 'employee':
employee = re.sub(r'[]\[\' ]','', line[1].strip()).split(',')
elif line[0] == 'salary':
salary = float(line[1])
elif line[0] == 'managers':
managers = re.sub(r'[]\[\' ]','', line[1].strip()).split(',')
print employee
print salary
print managers
yields:
['Tom', 'Bob', 'Anny']
200.0
['Saly', 'Alice']

Categories

Resources