Posts by Pedro von Hertwig Batista • 3,434 points
117 posts
-
2
votes1
answer535
viewsA: Find a specific element in a list with sub-lists (Python)
If I understand what you want to do, then maybe it’s better to use a dictionary with position tuples as keys: >>> posicoes = {(1, 3): [2, 1, 40, 90], (2, 3): [3, 2, 60, 50]} >>>…
-
0
votes1
answer181
viewsA: Python strip() function malfunction
Its string is in byte format, and only needs to be decoded to a standard format. >>> s = b'Mozilla/5.0 (BeOS; U; BeOS BePC; en-US; rv:1.8.1.6) Gecko/20070731 BonEcho/2.0.0.' >>>…
-
0
votes1
answer174
viewsA: Changing how Forms.Validationerror is displayed in Django
You can access the list of errors in your template. For example, to create tags h1 with the errors of a field name form: {% if form.name.errors %} {% for error in form.name.errors %} <h1…
-
0
votes2
answers78
viewsA: Request url wikipedia by date
I took the liberty of using requests_html instead of Beautifulsoup, but logic can be easily downloaded: from requests_html import HTMLSession from datetime import datetime import re url =…
-
0
votes1
answer19
viewsA: E-mail with update in the bank
After running the query, you need to commit to save the data to the database. sql_update = "UPDATE envia_email SET enviado = 'T' WHERE id = %i" % id # executa SQL cursor_update.execute(sql_update) #…
pythonanswered Pedro von Hertwig Batista 3,434 -
2
votes2
answers1322
viewsA: I understand a parallel run using Fork. How does Fork work?
Of official documentation: the.Fork() Fork a Child process. Return 0 in the Child and the Child’s process id in the Parent. If an error occurs Oserror is Raised. When the os.fork is called, the…
-
9
votes3
answers1588
viewsA: Python Floating Point Problem 3
Not a Python bug, but an inherent problem of how computers represent floating point. >>> 0.1 + 0.3 0.4 >>> 0.1 + 0.2 0.30000000000000004 Fortunately, Python offers us a simple way…
-
1
votes1
answer36
viewsA: Error when searching for a string
Your problem is on the following line: codPro = str(input('NOME DO PRODUTO: ')).upper().strip() With the upper, you make all the letters of the input given uppercase. So, if your product has…
-
2
votes1
answer2290
viewsA: How to use python round
The round takes its value to the nearest even integer. To round up, use the math.ceil: import math print(math.ceil(1.5)) # 2.0 print(math.ceil(2.5)) # 3.0 To round 0.5 up and 0.49999 down, you can…
-
1
votes1
answer282
viewsA: Get request in Python terminates the program when there is no connection
You just have to put in one while and take the exception when it occurs: def pega_ip(): while True: try: ip = get('https://api.ipify.org').text return ip except ConnectionError: pass So, if there is…
pythonanswered Pedro von Hertwig Batista 3,434 -
0
votes2
answers1526
viewsA: Ignore any white space in the middle of a string
A less ugly way is to let a function generate regex for you: def to_regex(s): return '\s*'.join(s) print(to_regex('teste'))
-
2
votes1
answer296
viewsA: Where is the source code of the Python3 commands located?
The source code for Cpython 3 is in that repository. You probably can’t find it because you have only compiled Python installed, which does not include the source code (Cpython, the main…
-
2
votes1
answer1153
viewsA: How to verify the existence of a file and the number of lines written in Python?
You can use the is_file of pathlib.Path to find out if the file exists. To count the lines, just discover the len of reading the file: from pathlib import Path caminho = Path('./arquivo.txt') if…
-
4
votes2
answers65
viewsA: Why can’t variables from the same instance be read in different processes? (Python)
You’re not actually accessing the same instance. When you create different processes, the entire state of the program is duplicated in memory and each process only modifies the instance in the…
-
0
votes1
answer73
viewsA: Problems with Opencv(python3 and ROS)
According to that page, there is no official distribution of ROS pro Python 3, but it may be possible to make it work if you compile the source ROS.…
-
0
votes1
answer648
viewsA: How to use Python Chatterbot in HTML page?
You’ll need to study a bit about how HTTP requests and forms of HTML to serve a page that communicates with your server. For the server itself, you have options like flask and the Django to receive…
-
1
votes1
answer9605
viewsA: Exporting data in json or txt, in python?
Come on, one thing at a time. The module json has two similar but different functions: dump, which accepts the object to be converted and an archive, and dumps, who accepts only the object and…