I'm pulling a dictionary from an API call, when I pull URL information it quotes and brackets around them [closed] - django

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I'm pulling the website information using a call in my Django template. All the information is getting pulled, but when I pull the website it has quotes and brackets around it like '[https://xxxxx.org]' obviously when I create the <a href=>{{ web.site }}</a> tag around it, it can't browse to the site.
Is there a quick way to strip off all the extra '[]' around the URL?

I figured it out, those were all good ideas, but what was happening was I was pulling from a dictionary with an embedded list.
'data': {
'BTC': {
'urls': {
'website': ['https://bitcoin.org/'],
'technical_doc': ['https://bitcoin.org/bitcoin.pdf'],
'twitter': [],
'source_code': ['https://github.com/bitcoin/']},
'logo': 'https://s2.coinmarketcap.com/static/img/coins/64x64/1.png',
'id': 1,
'name': 'Bitcoin',
'symbol': 'BTC',
I was only accessing up to the dictionary item:
{{ data.BTC.urls.technical_doc }}
Which was returning the list:
['https://bitcoin.org/bitcoin.pdf']
To get the item I wanted inside the list, I had to add a "0" at the end to snatch out that list item:
{{ data.BTC.urls.technical_doc.0 }}
Which in turn pulled the item out of the list clean.
https://bitcoin.org/bitcoin.pdf
Thanks for everyone's help, but it was my own misunderstanding.

i think this is what you need:
string.strip
string = "'[https://xxxxx.org]'"
# strip: Remove specific characters at the beginning and at the end of the string
print(string.strip("'[]"))
# Output: https://xxxxx.org

Related

I need to use RegEx to find a speciffic word in HTML page? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm trying to extract a specific word (that might change) which comes after a permanent expression. I want to extract the name Taldor in this code:
<h4 class="t-16 t-black t-normal">
<span class="visually-hidden">Company Name</span>
<span class="pv-entity__secondary-title">Taldor</span>
</h4>
For now I able to find <h4 class="t-16 t-black t-normal"> using this regex:
(?<=<h4 class="t-16 t-black t-normal">).*
Will be glad for any kind of advice.
I'd suggest you to use an HTML parsing library like Jsoup in Java or beautifulsoup in Python to parse HTML instead of using regex for this reason
Following is the kind of code that does the job for you,
String s = "<h4 class=\"t-16 t-black t-normal\">\r\n" +
" <span class=\"visually-hidden\">Company Name</span>\r\n" +
" <span class=\"pv-entity__secondary-title\">Taldor</span>\r\n" +
" </h4>";
Document doc = Jsoup.parse(s);
for (Element element : doc.getElementsByClass("pv-entity__secondary-title")) {
System.out.println(element.text());
break;
}
Prints,
Taldor
In worst case, if you are doing some quick and dirty work, you can do this temporary solution using regex but it is surely not recommended thing to do.
<span class="pv-entity__secondary-title">(.*?)<\/span>
Use this regex and capture your data from group1.
Demo

Remove HTML Comment Tags from text [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I'm really struggling trying to remove comment tags from HTML.
I want to keep everything inside the comment tags. I just want to remove <!-- and --> from the text.
I'm writing code using Python 2.7 and BeautifulSoup4.
I've tried using Regex to no avail. I tried the pattern "(<!--.*?-->)", but this seems to remove everything inside also.
I've also tried "(<!--|-->)" but it did not do what I wanted.
How can I achieve this?
you can use re.sub:
import re
f = open('filename.txt', 'r').readlines()
for n in f:
text = n.rstrip()
othertext = re.sub('<!--', '', text)
f = open('saved.txt', 'a')
f.write(othertext)
f.write('\n')
You can Just Group the Comments tag and replace the remaining data in the file
import re
List = "C:\\Users\\Administrator\\Desktop\\File1.txt"
with open(List,'r') as readfile:
content = readfile.readlines()
for i in content:
if '!' in i :
line = re.sub('(\<!--)([\w ]*)(-->)',r'\2',i)
with open('C:\\Users\\Administrator\\Desktop\\File2.txt',"a+") as writefile:
writefile.write(line)

In a Flask app, how to print each item of a list in the new paragraphs inside my HTML page [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm building a Flask app which is supposed to return all the items in a list as a new line in the HTML page.
For example:
list = [1,2,3,4]
I want to print each item in list as a new paragraph in my HTML page, like here:
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
You should better follow the flaskr tutorial on flask web site. It can give you the idea how to pass local variables to the template.
#app.route('/')
def your_view():
your_list= [1,2,3,4]
return render_template('your_view.html', your_list=your_list)
then in your jinja template, iterate over this list.
{% for your_list_element in your_list %}
<p>{{ your_list_element }} </p>
{% endfor %}

preg_replace regular expression HTML [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am using shortcodes in WordPress. After each shortcode output (closing div) I got <br> (or <br />) tag.
trying to filter them out, but I don't know how. Generated HTML looks like
<div class="fullwidth"><!-- 1st shortcode-->
<div class="fullwidth-content">
<!-- 2nd shortcode-->
<div class="twocol-one"> content
</div><br>
</div><br>
<!-- 3rd shortcode-->
<div class="twocol-second"> content
</div><br>
<div class="clearboth"></div>
</div><br>
seems BR is newline from tinyMCE. And I don't want loooong shotcode lines.
I am trying to use preg_replace but i cannot create correct $pattern.
Can you help me?
my function
function replace_br($content) {
$rep = preg_replace("/<\/div>\s*<br\s*\/?>/i", "</div>",$content);
return $rep; }
add_filter('the_content', 'replace_br');
not working.
While using
$rep = preg_replace("/\s*<br\s*\/?>/i", "",$content); in function, all BRs are replaced.
Fine, but i want to replace only BRs after closing DIV tag.
str_replace("</div><br>", "</div>", $content); also not working.
What's wrong with my function?
No error returned.
You are doing it wrong in the first place, since you have to remove the tags.
You are doing it wrong because you're using regex for HTML (sometimes it's OKish).
Variation of regex you're using should suffice: Demo
You should really consider using DOMDocument or similar:
$html = <<<HTML
...
HTML;
$dom = new DOMDocument();
$dom->loadHTML($html);
$element = $dom->getElementsByTagName('br');
$remove = [];
foreach($element as $item){
$remove[] = $item;
}
foreach ($remove as $item) {
$item->parentNode->removeChild($item);
}
$html = $dom->saveHTML();
echo $html;
This would remove all of br, you would need to adjust the code work for your specs, but this should be a pointer.
this is an alternative way to use regex
in your case
/(?<=<\/div>)(<br[\s\/]?>)/mg

Why won't Django auto-escape my <script> tags?

My Django app has a Person table, which contains the following text in a field named details:
<script>alert('Hello');</script>
When I call PersonForm.details in my template, the page renders the script accordingly (a.k.a., an alert with the word "Hello" is displayed). I'm confused by this behavior because I always thought Django 1.0 autoescaped template content by default.
Any idea what may be going on here?
UPDATE: Here's the snippet from my template. Nothing terribly sexy:
{{ person_form.details }}
UPDATE 2: I have tried escape, force-escape, and escapejs. None of these work.
You need to mark the values as | safe I think (I'm guessing that you're filling in the value from the database here(?)):
{{ value|safe }}
Could you post a sample of the template? Might make it easier to see what's wrong
[Edit] ..or are you saying that you want it to escape the values (make them safe)? Have you tried manually escaping the field:
{{ value|escape }}
[Edit2] Maybe escapejs from the Django Project docs is relevent:
escapejs
New in Django 1.0.
Escapes characters for use in JavaScript strings. This does not make the string safe for use in HTML, but does protect you from syntax errors when using templates to generate JavaScript/JSON.
[Edit3] What about force_escape:
{{ value|force_escape }}
...and I know it's an obvious one, but you're absolutely certain you've not got any caching going on in your browser? I've tripped over that one a few times myself ;-)
Found the problem. The JSON string I'm using to render data to some Ext widgets is the culprit. Big thanks to Jon Cage. Answer accepted despite the problem being caused by another source.