How to format text written to a variable? - django

I am get the text from the database. He comes in the following form.
city = City.objects.first()
#London
country = Contry.objects.first()
#UK
metatag = MetaTag.objects.first()
#text {city.name} else text {country.name}
screen of note from database
How can I format it?
Need to get:
text London else text UK

metatag.format(city_name=city.name, country_name=country.name)

Related

c# windows form - How received td area data in my textbox or listbox?

your boss name after finish b tag Want received random generated name so we need this name in our textbox or list box area.
Actually every loading time we see your boss name after coming new name so we just received name in our textbox area or list box are.
Please help me how I am received name data in my list box or textbox Please.
Your boss name adriana
your id: 2241
Email: yourbossemail#gmail.com
File Format: officeial
Your boss name jennefer
your id: 2241
Email: yourbossemail#gmail.com
File Format: officeial
my code
HtmlElementCollection bColl = webBrowser1.Document.GetElementsByTagName("td");
foreach (HtmlElement bEl in bColl)
{
if (bEl.GetAttribute("b").Contains("your"))
txtLinkDetails.Text = bEl.OuterHtml.Split('"')[3].Replace("&amp", "&");
}
Try this:
at first you find innerhtml of table then filter by <b>([^<]+)<\/b>([^<]+) regex
Demo
And you will get following output with array group:
//example
//group 1 =Your boss name
// group 2= jennefer

Check if a string value matches up with the content of an existing table in Oracle 11G

At the moment I am not working as efficient as I could be. For the problem I have I almost know certain that there is a smarter and better way to fix it.
What I am trying to do:
I got a string like this:
'NL 4633 4809 KTU'
The NL is a country code from an existing table and KTU is an university code from an existing table. I need to put this string in my function and check if the string is validated.
In my function (to validate the string) this is what I am working on. I have managed to split up the string with this:
countryCode := checkISIN; -- checkISIN is the full string ('NL 4633 4809 KTU') and I am giving the NL value to this variable. countryCode is the type varchar2(50)
countryCode := regexp_substr(countryCode, '[^ ]+', 1, 1);
Now that I have the country code as shown below:
NL
Has valid country code
I want to validate/check the country code for it's existence from it's own table. I tried this:
if countryCode in ('NL', 'FR', 'DE', 'GB', 'BE', 'US', 'CA')
then dbms_output.put_line('Has valid country code');
else
dbms_output.put_line('Has invald country code. Change the country code to a valid one');
end if;
This works, but it's not dynamically. If someone adds a country code then I have to change the function again.
So is there a (smart/dynamically) way to check the country codes for their existing tables?
I hope my question is not too vague
Cheers
If you have Country codes table and it looks like this:
ID | NAME
----------
1 | NL
2 | FR
3 | BE
when you parse string, you can make like this :
select count(1)
into v_quan
from CountryCodes cc
where nvl(countryCode,'') = cc.name
if v_quan > 0 then
dbms_output.put_line('Has valid country code');
else
dbms_output.put_line('Has invald country code. Change the country code to a valid one');
end if;

Regular Expression to deal with text issue

I have the following text sample:
Sample Supplier 123
AP Invoices 123456 -229.00
AP Invoices 235435 337.00
AP Invoices 444323 228.00
AP Invoices 576432 248.00
It's from a text file with 21,000 lines, which lists invoices against a supplier.
The pattern is always the same on each block of invoices against each supplier, where:
The supplier name starts at the beginning of a line
The invoices being to be listed 2 rows down from the supplier name, indented by one space.
I wondered if I can use a Regular Expression (I'm using TextPad as a Text Editor on a Windows PC) to:
Append each invoice line with a tab (\t)
Append the supplier name in front of the tab so each invoice line now starts with the supplier name, and a tab, where the supplier name is taken from 2 rows above the start of each block of invoices
Delete the supplier name line from above the invoice block.
Expected output:
Sample Supplier 123 AP Invoices 123456 -229.00
Sample Supplier 123 AP Invoices 235435 337.00
Sample Supplier 123 AP Invoices 444323 228.00
Sample Supplier 123 AP Invoices 576432 248.00
I realise I am probably asking for "the moon on a stick" here, but the alternative is to go through a 21,000 line text file and copy and paste the data into Excel, which might not be a very good use of my time.
Maybe I can't do it using a simple regular expression, or maybe it's simply not possible at all.
Any advice would be much appreciated.
Thanks
I would use a simple Python script to solve this issue:
currentheader = ""
with open("yourfile.txt") as f:
with open("newfile.txt","w") as fw:
for line in f:
if len(line.strip()) == 0:
continue
elif line[0] != " ": #new header
currentheader = line[:-1]
else:
fw.write(currentheader + "\t" + line[1:])
For this to work, on Windows you will have to install Python. Python 2 or 3 should both work with this script. After installing Python, you open a command line (Win+R, cmd, Enter), navigate to the folder your file is in using cd foldername, if necessary, and then type python dealWithTextIssue.py (after having saved the script as "dealWithTextIssue.py" there.
I think this isn't solvable just with regex, you'll have to do some programming. I made a little script in PHP:
$string = <<<EOL
Sample Supplier 123
AP Invoices 123456 -229.00
AP Invoices 235435 337.00
AP Invoices 444323 228.00
AP Invoices 576432 248.00
Second Supplier
A B C D
B F
EOL;
$array = preg_split("~[\n\r]+~", $string);
foreach ($array as $value) {
if (strpos($value, " ") == 0) {
if (strlen(trim($value)) > 0) {
echo "\t".$header.rtrim($value).PHP_EOL;
}
}
else {
$header = $value;
}
}
You can see it at work for example here after clicking on execute code.

Extract Information from Text in R

I am working on Entity Extraction in R.
I have a UniqueID and Text field - need to extract location information from the text field.
My Text field has description with location names
text <- c("SERANGOON JC","Blk 4","SHELL TAMPINES AVE 4","SENOKO INDUSTRIAL ESTATE","Senoko Estate","Senoko","senok Est.")
I have a list of Locations ;
Loc <- c("SERANGOON JUNIOR COLLEGE","Block 4","SHELL TAMPINES AVENUE 4","SENOKO INDUSTRIAL ESTATE")
Need to match the loc and extract those location from the text field.In the text field SENOKO INDUSTRIAL ESTATE is spelt in different ways Senoko Estate or Senoko (Half Names) or with spelling mistake senok Est. .for all the above mis-spelt and half spelt words - i need to get the exact name from loc ie. SENOKO INDUSTRIAL ESTATE.
My output would look like:(Extract location from Text field -get correct words for half- spelt and misspelt words)
ID Location
123 SERANGOON JUNIOR COLLEGE|Block 4|SHELL TAMPINES AVENUE 4|SENOKO INDUSTRIAL ESTATE|SENOKO INDUSTRIAL ESTATE|SENOKO INDUSTRIAL ESTATE|SENOKO INDUSTRIAL ESTATE
I don't think this is the prettiest way to answer it, but..
text <- c("SERANGOON JC","Blk 4","SHELL TAMPINES AVE 4","SENOKO INDUSTRIAL ESTATE","Senoko Estate","Senoko","senok Est.")
Loc <- c("SERANGOON JUNIOR COLLEGE","Block 4","SHELL TAMPINES AVENUE 4","SENOKO INDUSTRIAL ESTATE")
text <- gsub(".*serang.*", "SERANGOON JUNIOR COLLEGE", text, ignore.case=TRUE)
text <- gsub(".*bl.* 4.*", "Block 4", text, ignore.case=TRUE)
text <- gsub(".*shell.*", "SHELL TAMPINES AVENUE 4", text, ignore.case=TRUE)
text <- gsub(".*senok.*", "SENOKO INDUSTRIAL ESTATE", text, ignore.case=TRUE)
print(text)
I didn't put it exactly in the format you requested, but that would be the contents of the second column (aka Location). I used the regex expression ".*" before and after the strings you were looking for in case there are other possibilities/typos. This would make it more robust.
Hope this helps!

What's wrong with this for scraping the table and data needed?

I'm trying to scrape data for Miami Heat and their opponent from a table at http://www.scoresandodds.com/grid_20111225.html. The problem I have is that tables for NBA and NFL and other sports are all identicaly marked and all the data I get is from the NFL table. Another problem is that I would like to scrape data for the entire season and the number of different tables changes and the position of Miami changes in the table. This is the code I've been using for different tables till now;
So why is this not getting the job done? Thx for you patience; I'm a real begginer, and I've been trying to solve this problem for some days now, to no effect.
def tableSnO(htmlSnO):
gameSections = soup.findAll('div', 'gameSection')
for gameSection in gameSections:
header = gameSection.find('div', 'header')
if header.get('id') == 'nba':
rows = gameSections.findAll('tr')
def parse_string(el):
text = ''.join(el.findAll(text=True))
return text.strip()
for row in rows:
data = map(parse_string, row.findAll('td'))
return data
Lately I decided to try a different approach; if I scrape the entire page and get the index of the data in question (this is where it stops:) I could just get the next set of data from the list, since that structure of the table never changes. I could also get the opponent's team name the same way I get the htmlSnO . It feels like this is such basic stuff and it's killing me that I can't get it right.
def tableSnO(htmlSnO):
oddslist = soupSnO.find('table', {"width" : "100%", "cellspacing" : "0", "cellpadding" : "0"})
rows = oddslist.findAll('tr',)
def parse_string(el):
text = ''.join(el.findAll(text=True))
return text.strip()
for row in rows:
data = map(parse_string, row.findAll('td'))
for teamName in data:
if re.match("(.*)MIAMI HEAT(.*)", teamName):
return teamName
return data.index(teamName)
New and final answer with working code:
The section of the page you want has this:
<div class="gameSection">
<div class="header" id="nba">
This should let you get at the NBA tables:
def tableSnO(htmlSnO):
gameSections = soup.findAll('div', 'gameSection')
for gameSection in gameSections:
header = gameSection.find('div', 'header')
if header.get('id') == 'nba':
# process this gameSection
print gameSection.prettify()
As a complete example, here's the full code I used to test:
import sys
import urllib2
from bs4 import BeautifulSoup
f = urllib2.urlopen('http://www.scoresandodds.com/grid_20111225.html')
html = f.read()
soup = BeautifulSoup(html)
gameSections = soup.findAll('div', 'gameSection')
for gameSection in gameSections:
header = gameSection.find('div', 'header')
if header.get('id') == 'nba':
table = gameSection.find('table', 'data')
print table.prettify()
This prints the NBA data table.