i have been trying to implement a search bar. My idea is that the user can partially type the word and then i can pass the variable in the response so it is used in my EJS. I am not using SQL databases on purpose, as im trying to keep this site running only with JSON file as database. I have tried but cant seem to make it work. I have tried many ways.
let userSearch = (req.body.search)
let allProducts = data
let filteredProducts = allProducts.filter(x => x.title == x.title.indexOf(userSearch))
console.log(allProducts), gives you this :
[
{
id: 1,
title: 'Chicken and vegetables stir-fry',
Ingredients: [
'2 tbsp peanut oil',
'3 Lilydale Free Range Chicken Breast, trimmed, cut across the grain into thin strips',
'1 brown onion, cut into thin wedges',
'1 red capsicum, deseeded, cut into thin strips',
'250g button mushrooms or mushroom caps, sliced',
'1 bunch broccolini, cut into 4cm lengths',
'2cm piece fresh ginger, peeled, finely grated or chopped',
'2 garlic cloves, finely chopped',
'2 fresh birdseye chillies, deseeded if desired, thinly sliced',
'2 tbsp salt-reduced soy sauce',
'1 1/2 tbsp oyster sauce',
'1 tbsp water',
'100g bean sprouts, to serve',
'Coriander leaves, to serve',
'Cooked white rice or noodles, to serve'
],
description: 'Delicious and nutricious',
image: 'chickenAndVeggies.jpeg',
belongsTo: 'maria#hotmail.com'
},
{
id: 2,
title: 'Chocolate fudge cake',
Ingredients: [
'2 150ml sunflower oil, plus extra for the tin',
'175g self-raising flour',
'2 tbsp cocoa powder',
'1 tsp bicarbonate of soda',
'150g caster sugar',
'2 tbsp golden syrup',
'2 large eggs, lightly beaten',
'150ml semi-skimmed milk'
],
description: 'What´s better than a choco cake?',
image: 'chococake.jpeg',
belongsTo: 'maria#hotmail.com'
},
{
id: 3,
title: 'Quiche Lorraine',
Ingredients: [
'1/8 tsp ground nutmeg',
'1/4 tsp smoked paprika',
'1/4 tsp black pepper, finely ground',
'1/2 tsp kosher salt',
'4 eggs, large',
'1 3/4 cup half and half, (or equal parts whole milk and heavy cream)',
'1 cup shredded cheese , (I used sharp white cheddar, but gruyere is classic)',
'1/2 yellow onion, diced',
'1/2 lb thin strips of bacon, chopped into 1/2 inch pieces',
'1 pie crust, (1 frozen crust, or 1/2 recipe for double pie crust)'
],
description: 'A delicious quiche',
image: 'QuicheLorraine.jpeg',
belongsTo: 'richmond#hotmail.com'
},
{
id: 4,
title: 'Fluffy pancakes',
Ingredients: [
'1 cup all-purpose flour',
'2 tablespoons white sugar',
'2 teaspoons baking powder',
'1 teaspoon salt',
'1 egg, beaten',
'1 cup milk',
'2 tablespoons vegetable oil'
],
description: 'Easy to make, delicious to eat',
image: 'fluffypancakes.jpeg',
belongsTo: 'johnny#hotmail.com'
},
{
id: 5,
title: 'Carrot cake',
Ingredients: [
'2 cups (240g) all-purpose flour',
'2 teaspoons baking powder',
'2 teaspoons cinnamon',
'2 teaspoons allspice ',
'1/2 teaspoon salt',
'4 large eggs',
'2 cups (400g) granulated sugar',
'1 1/4 cup vegetable oil',
'3 cups loosely packed peeled and grated carrots'
],
description: 'mmmm a dream!',
image: 'carrot-cake.jpeg',
belongsTo: 'richmond#hotmail.com'
},
{
id: 6,
title: 'Kitty kitty',
description: 'Beautiful',
Ingredients: [ 'kitty', ' nothing', ' test', ' test', ' test' ],
image: 'image-1654609682017.jpeg',
belongsTo: 'maria#hotmail.com'
}
]
console.log(userSearch) gives you the input you typed in the search bar input, the one coming in the body. In this case I typed hello so:
hello
console.log(filteredProducts) gives an empty array regardless
[]
This for example, returns an empty array even when copying the title exaclty.
Any good ideas on how to implement the search bar in this case?
P.S i don´t know Jquery yet i use simple JS with NodeJs. thanks!
Related
I have a bash array as follows:
fruits=(
F001 "Sour fruit" 5
F002 "Sweet fruit" 15
F003 "Good fruit" 10
)
I want to access this array and print it as follows:
Fruit code: F001, Desc: Sour fruit, Price: 5
Fruit code: F002, Desc: Sweet fruit, Price: 15
Fruit code: F003, Desc: Good fruit, Price: 10
I tried the following code, But it's giving me a wrong output
for f in "${fruits[#]}"; do
IFS=" " read -r -a arr <<<"${f}"
code="${arr[0]}"
desc="${arr[1]}"
price="${arr[2]}"
echo "Fruit code: : ${code}, Desc: ${desc}, Price: ${price}"
echo
done
Anyine know how to do this :)
The bash array you define does not have 3 elements, but 9. So you can quickly do this with a modulo 3 operation:
for ((i=0;i<"${#fruits[#]}";i=i+3)); do
code="${fruits[i]}"
desc="${fruits[i+1]}"
price="${fruits[i+2]}"
echo "Fruit code: : ${code}, Desc: ${desc}, Price: ${price}"
done
Hi I have the following string, how can I get the B000RMTGUQ of this entire string using regex pytnon?
{'asin': 'B000RMTGUQ', 'imUrl': 'http://ecx.images-amazon.com/images/I/515KlX4dEUL._BO2,204,203,200_PIsitb-sticker-v3-big,TopRight,0,-55_SX278_SY278_PIkin4,BottomRight,1,22_AA300_SH20_OU01_.jpg', 'related': {'also_bought': ['B009QJMXI8', 'B00CNQ7MJG']}, 'categories': [['Books', 'History', 'World', 'Jewish', 'Holocaust'], ['Books', 'History', 'World', 'Religious', 'Judaism'], ['Books', 'Politics & Social Sciences', 'Social Sciences'], ['Books', 'Religion & Spirituality', 'Judaism'], ['Kindle Store', 'Kindle eBooks', 'History', 'World', 'Jewish', 'Holocaust'], ['Kindle Store', 'Kindle eBooks', 'Politics & Social Sciences', 'Social Sciences'], ['Kindle Store', 'Kindle eBooks', 'Religion & Spirituality', 'Judaism']], 'description': "Kibbutz Buchenwald was founded in Germany in 1945 by 16 survivors of Buchenwald concentration camp. The Zionist training farm was organized to prepare Jews for emigration to Palestine. One of the founders was Yeohezkel Tydor, the author's father, who died in 1993. Baumel's narration of the kibbutz's history is divided into two sections. Part one examines the kibbutz from its creation until the departure of the founding group to Palestine in late summer 1945. Part two traces the kibbutz's subsequent history in Palestine and Germany, from the autumn of 1945 until the mid-1950s. Kibbutz Buchenwald was abolished in Germany in 1948; the kibbutz as it was founded in what is now Israel--named Netzer Sereni--still exists today. The story of these pioneers and their physical, psychological, ideological, and political struggles forms the nucleus of this absorbing book.George Cohen"}
You could use json to parse your string to a valid dictionary:
First note that a valid json is enclosed by double quotes. Also, note that author"s etc need single quotes. Hence you could do:
import json, re
dct = json.loads(re.sub('"s', "'s", re.sub("'", '"', string)))
dct['asin']
'B000RMTGUQ'
EDIT
from the comments below, it seems you do not have a json string but rather a valid python dictionary in string format:
therefore you could directly do:
dc = eval(string)
dc['asin']
Futher more consider using ast.literal_eval rather than eval.
data
string = """{'asin': 'B000RMTGUQ', 'imUrl': 'http://ecx.images-amazon.com/images/I/515KlX4dEUL._BO2,204,203,200_PIsitb-sticker-v3-big,TopRight,0,-55_SX278_SY278_PIkin4,BottomRight,1,22_AA300_SH20_OU01_.jpg', 'related': {'also_bought': ['B009QJMXI8', 'B00CNQ7MJG']}, 'categories': [['Books', 'History', 'World', 'Jewish', 'Holocaust'], ['Books', 'History', 'World', 'Religious', 'Judaism'], ['Books', 'Politics & Social Sciences', 'Social Sciences'], ['Books', 'Religion & Spirituality', 'Judaism'], ['Kindle Store', 'Kindle eBooks', 'History', 'World', 'Jewish', 'Holocaust'], ['Kindle Store', 'Kindle eBooks', 'Politics & Social Sciences', 'Social Sciences'], ['Kindle Store', 'Kindle eBooks', 'Religion & Spirituality', 'Judaism']], 'description': "Kibbutz Buchenwald was founded in Germany in 1945 by 16 survivors of Buchenwald concentration camp. The Zionist training farm was organized to prepare Jews for emigration to Palestine. One of the founders was Yeohezkel Tydor, the author's father, who died in 1993. Baumel's narration of the kibbutz's history is divided into two sections. Part one examines the kibbutz from its creation until the departure of the founding group to Palestine in late summer 1945. Part two traces the kibbutz's subsequent history in Palestine and Germany, from the autumn of 1945 until the mid-1950s. Kibbutz Buchenwald was abolished in Germany in 1948; the kibbutz as it was founded in what is now Israel--named Netzer Sereni--still exists today. The story of these pioneers and their physical, psychological, ideological, and political struggles forms the nucleus of this absorbing book.George Cohen"}"""
I have a text file which have content like :
Name: Aar saa
Last Name: sh
DOB: 1997-03-22
Phone: 1212222
Graduation: B.Tech
Specialization: CSE
Graduation Pass Out: 2019
Graduation Percentage: 60
Higher Secondary Percentage: 65
Higher Secondary School Name: Guru Nanak Dev University,amritsar
City: hyd
Venue Details: CMR College of Engineering & Technology (CMRCET) Medchal Road, TS � 501401
Name: bfdg df
Last Name: df
DOB: 2005-12-16
Phone: 2222222
Graduation: B.Tech
Specialization: EEE
Graduation Pass Out: 2018
Graduation Percentage: 45
Higher Secondary Percentage: 45
Higher Secondary School Name: asddasd
City: vjd
Venue Details: Prasad V. Potluri Siddhartha Institute Of Technology, Kanuru, AP - 520007
Name: cc dd ee
Last Name: ee
DOB: 1995-07-28
Phone: 444444444
Graduation: B.Tech
Specialization: ECE
Graduation Pass Out: 2019
Graduation Percentage: 75
Higher Secondary Percentage: 93
Higher Secondary School Name: Sasi institute of technology and engineering
City: hyd
Venue Details: CMR College of Engineering & Technology (CMRCET) Medchal Road, TS � 501401
I want to convert it CSV file with headers as
['Name', 'Last Name','DOB', 'Phone', 'Graduation','Specialization','Graduation Pass Out','Higher Secondary School Name','City','Venue Details']
with value as all the value after ':'
I have done something like this:
writer = csv.writer(open('result.csv', 'a'))
writer.writerow(['Name', 'Last Name','DOB', 'Phone', 'Graduation','Specialization','Graduation Pass Out','Graduation Percentage','Higher Secondary Percentage','Higher Secondary School Name','City','Venue Details'])
with open('Name2.txt') as f:
text = f.read()
myarray = text.split("\n\n")
for text1 in myarray:
parselines(text1, writer)
def parselines(lines,writer):
data=[]
for line in lines.split('\n'):
Name = line.split(": ",1)[1]
data.append(Name)
writer.writerow(data)
It worked but any efficient way would be much appreciated.
This algorithm works (kind-of a state machine)
If blank line, make a new row
Otherwise: add to current row, collect all headers and fields
def parselines(lines):
header = []
csvrows = [{}]
for line in lines:
line = line.strip()
if not line:
csvrows.append({}) # new row, in dict form
else:
field, data = line.split(":", 1)
csvrows[-1][field] = data
if field not in header:
header.append(field)
# format CSV
print(",".join(header))
for row in csvrows:
print(",".join(row.get(h,"") for h in header))
I would like to know how to use the transverse_tagged_database method in brightway2. From the documentation is not entirely clear to me. Can we use for, example, to aggregate impacts by the isic code of the activities in the product system model?
The short answer is yes, aggregating impacts by ISIC code in your foreground product system model is exactly the kind of thing you can do using traverse_tagged_databases.
The traverse_tagged_databases function takes advantage of the fact that you can add arbitrary key:value pairs to activities in brightway to let you classify your the activities in your foreground model however you like.
For example, say your activities look like this:
('example_database', 'code_for_bread'):{
'name': 'Bread',
'code': 'code_for_bread',
'categories':[],
'exchanges':[...],
'location':'GLO'
'unit':'kg',
'database':'example_database',
'isic_code':'1071'
'isic_classifier':'Manufacture of bakery products'
},
You can tell traverse_tagged_databases to go through your database looking for a given key (tag), for example 'isic_code', or 'isic_classifier' and aggregate the impact based on these tags.
Say you were modelling a cheese sandwich, you could have the following ISIC codes in your model:
Sandwich: 1079 (Manufacture of other food products n.e.c.)
Bread: 1071 (Manufacture of bakery products)
Cheese: 1050 (Manufacture of dairy products)
Butter: 1050 (Manufacture of dairy products)
You can use traverse_tagged_databases to see the total impact of dairy (cheese and butter) vs bakery (bread).
You use it in a similar way to the LCA function, by specifying a functional unit as a dict and the method as a tuple, with an additional tag argument. Like this:
fu = {('example_database', 'code_for_sandwich'):1}
m = ('IPCC 2013', 'climate change', 'GWP 100a')
result, tree = traverse_tagged_databases(fu, m, 'isic_classifier')
The function returns two objects (designated result and tree in the line above)
For this analysis, your result will look something like this:
defaultdict(int,
{'Manufacture of other food products n.e.c.': 0,
'Manufacture of bakery products': 0.1875,
'Manufacture of dairy products': 0.55})
This is saying that dairy products in the foreground model have an aggregated impact of 0.55 kg CO2-eq, and bakery products have an aggregated impact of 0.1875 kg CO2-eq.
With a bit of post-processing you can turn this data into pie charts, stacked bar charts etc.
You also get a tree, which looks like this:
[{'activity': 'Sandwich' (kg, GLO, []),
'amount': 1,
'tag': 'Manufacture of other food products n.e.c.',
'impact': 0,
'biosphere': [],
'technosphere': [{'activity': 'Bread' (kg, GLO, []),
'amount': 0.75,
'tag': 'Manufacture of bakery products',
'impact': 0,
'biosphere': [{'amount': 0.1875,
'impact': 0.1875,
'tag': 'Manufacture of bakery products'}],
'technosphere': []},
{'activity': 'Butter' (kg, GLO, []),
'amount': 0.05,
'tag': 'Manufacture of dairy products',
'impact': 0,
'biosphere': [{'amount': 0.05,
'impact': 0.05,
'tag': 'Manufacture of dairy products'}],
'technosphere': []},
{'activity': 'Cheese' (kg, GLO, []),
'amount': 0.25,
'tag': 'Manufacture of dairy products',
'impact': 0,
'biosphere': [{'amount': 0.5,
'impact': 0.5,
'tag': 'Manufacture of dairy products'}],
'technosphere': []}]}]
This can look a bit difficult to parse at first, but is essentially a set of nested dictionaries, starting with the root activity (the functional unit = Sandwich), showing techosphere exchanges to other activities, and biosphere exchanges to emissions.
The tree here looks like this (with the amounts of each product in brackets)
Bread +----(0.75 kg)----------+
|
|
|
|
Cheese +----(0.20 kg)----------+------(1.00 kg)--------> Sandwich
|
|
|
|
Butter +----(0.05 kg)----------+
Again, with a bit of post-processing, you can turn this data into stuff like sankey diagrams, or the kind of impact tree diagram you get in SimaPro.
I have this csv file with only two entries. Here it is:
Meat One,['Abattoirs', 'Exporters', 'Food Delivery', 'Butchers Retail', 'Meat Dealers-Retail', 'Meat Freezer', 'Meat Packers']
First one is title and second is a business headings.
Problem lies with entry two.
Here is my code:
import csv
with open('phonebookCOMPK-Directory.csv', "rt") as textfile:
reader = csv.reader(textfile)
for row in reader:
row5 = row[5].replace("[", "").replace("]", "")
listt = [(''.join(row5))]
print (listt[0])
it prints:
'Abattoirs', 'Exporters', 'Food Delivery', 'Butchers Retail', 'Meat Dealers-Retail', 'Meat Freezer', 'Meat Packers'
What i need to do is that i want to create a list containing these words and then print them like this using for loop to print every item separately:
Abattoirs
Exporters
Food Delivery
Butchers Retail
Meat Dealers-Retail
Meat Freezer
Meat Packers
Actually I am trying to reformat my current csv file and clean it so it can be more precise and understandable.
Complete 1st line of csv is this:
Meat One,+92-21-111163281,Al Shaheer Corporation,Retailers,2008,"['Abattoirs', 'Exporters', 'Food Delivery', 'Butchers Retail', 'Meat Dealers-Retail', 'Meat Freezer', 'Meat Packers']","[[' Outlets Address : Shop No. Z-10, Station Shopping Complex, MES Market, Malir-Cantt, Karachi. Landmarks : MES Market, Station Shopping Complex City : Karachi UAN : +92-21-111163281 '], [' Outlets Address : Shop 13, Ground Floor, Plot 14-D, Sky Garden, Main Tipu Sultan Road, KDA Scheme No.1, Karachi. Landmarks : Nadra Chowrangi, Sky Garden, Tipu Sultan Road City : Karachi UAN : +92-21-111163281 '], ["" Outlets Address : Near Jan's Broast, Boat Basin, Khayaban-e-Roomi, Block 5, Clifton, Karachi. Landmarks : Boat Basin, Jans Broast, Khayaban-e-Roomi City : Karachi UAN : +92-21-111163281 View Map ""], [' Outlets Address : Gulistan-e-Johar, Karachi. Landmarks : Perfume Chowk City : Karachi UAN : +92-21-111163281 '], [' Outlets Address : Tee Emm Mart, Creek Vista Appartments, Khayaban-e-Shaheen, Phase VIII, DHA, Karachi. Landmarks : Creek Vista Appartments, Nueplex Cinema, Tee Emm Mart, The Place City : Karachi Mobile : 0302-8333666 '], [' Outlets Address : Y-Block, DHA, Lahore. Landmarks : Y-Block City : Lahore UAN : +92-42-111163281 '], [' Outlets Address : Adj. PSO, Main Bhittai Road, Jinnah Supermarket, F-7 Markaz, Islamabad. Landmarks : Bhittai Road, Jinnah Super Market, PSO Petrol Pump City : Islamabad UAN : +92-51-111163281 ']]","Agriculture, fishing & Forestry > Farming equipment & services > Abattoirs in Pakistan"
First column is Name
Second column is Number
Third column is Owner
Forth column is Business type
Fifth column is Y.O.E
Sixth column is Business Headings
Seventh column is Outlets (List of lists containing every branch address)
Eighth column is classification
There is no restriction of using csv.reader, I am open to any technique available to clean my file.
Think of it in terms of two separate tasks:
Collect some data items from a ‘dirty’ source (this CSV file)
Store that data somewhere so that it’s easy to access and manipulate programmatically (according to what you want to do with it)
Processing dirty CSV
One way to do this is to have a function deserialize_business() to distill structured business information from each incoming line in your CSV. This function can be complex because that’s the nature of the task, but still it’s advisable to split it into self-containing smaller functions (such as get_outlets(), get_headings(), and so on). This function can return a dictionary but depending on what you want it can be a [named] tuple, a custom object, etc.
This function would be an ‘adapter’ for this particular CSV data source.
Example of deserialization function:
def deserialize_business(csv_line):
"""
Distills structured business information from given raw CSV line.
Returns a dictionary like {name, phone, owner,
btype, yoe, headings[], outlets[], category}.
"""
pieces = [piece.strip("[[\"\']] ") for piece in line.strip().split(',')]
name = pieces[0]
phone = pieces[1]
owner = pieces[2]
btype = pieces[3]
yoe = pieces[4]
# after yoe headings begin, until substring Outlets Address
headings = pieces[4:pieces.index("Outlets Address")]
# outlets go from substring Outlets Address until category
outlet_pieces = pieces[pieces.index("Outlets Address"):-1]
# combine each individual outlet information into a string
# and let ``deserialize_outlet()`` deal with that
raw_outlets = ', '.join(outlet_pieces).split("Outlets Address")
outlets = [deserialize_outlet(outlet) for outlet in raw_outlets]
# category is the last piece
category = pieces[-1]
return {
'name': name,
'phone': phone,
'owner': owner,
'btype': btype,
'yoe': yoe,
'headings': headings,
'outlets': outlets,
'category': category,
}
Example of calling it:
with open("phonebookCOMPK-Directory.csv") as f:
lineno = 0
for line in f:
lineno += 1
try:
business = deserialize_business(line)
except:
# Bad line formatting?
log.exception(u"Failed to deserialize line #%s!", lineno)
else:
# All is well
store_business(business)
Storing the data
You’ll have the store_business() function take your data structure and write it somewhere. Maybe it’ll be another CSV that’s better structured, maybe multiple CSVs, a JSON file, or you can make use of SQLite relational database facilities since Python has it built-in.
It all depends on what you want to do later.
Relational example
In this case your data would be split across multiple tables. (I’m using the word “table” but it can be a CSV file, although you can as well make use of an SQLite DB since Python has that built-in.)
Table identifying all possible business headings:
business heading ID, name
1, Abattoirs
2, Exporters
3, Food Delivery
4, Butchers Retail
5, Meat Dealers-Retail
6, Meat Freezer
7, Meat Packers
Table identifying all possible categories:
category ID, parent category, name
1, NULL, "Agriculture, fishing & Forestry"
2, 1, "Farming equipment & services"
3, 2, "Abattoirs in Pakistan"
Table identifying businesses:
business ID, name, phone, owner, type, yoe, category
1, Meat One, +92-21-111163281, Al Shaheer Corporation, Retailers, 2008, 3
Table describing their outlets:
business ID, city, address, landmarks, phone
1, Karachi UAN, "Shop 13, Ground Floor, Plot 14-D, Sky Garden, Main Tipu Sultan Road, KDA Scheme No.1, Karachi", "Nadra Chowrangi, Sky Garden, Tipu Sultan Road", +92-21-111163281
1, Karachi UAN, "Near Jan's Broast, Boat Basin, Khayaban-e-Roomi, Block 5, Clifton, Karachi", "Boat Basin, Jans Broast, Khayaban-e-Roomi", +92-21-111163281
Table describing their headings:
business ID, business heading ID
1, 1
1, 2
1, 3
…
Handling all this would require a complex store_business() function. It may be worth looking into SQLite and some ORM framework, if going with relational way of keeping the data.
You can just replace the line :
print(listt[0])
with :
print(*listt[0], sep='\n')