Posts by JJoao • 5,113 points
222 posts
-
0
votes2
answers893
viewsA: How to remove part of a JSON file
(not really an answer but a note) Using the csvkit: $ in2csv -Ik value outro.json cprq_cert_id,cprq_object_id,cprq_cert_cert_id 51,d0b7fae2-cc11-44d1-a45b-a39642e65c08,0…
-
1
votes4
answers120
viewsA: How to filter an HTML tag and its contents with regular expressions in Shell Bash?
Using regular expressions via Perl $ perl -nE '/<span.*?>(.*?)- TorraTudo.*8250; (.*?)<.span>/ and say $1,$2' file.html Colcha Casal e ... cama Colcha Solteiro e ... cama Roupão de banho…
-
0
votes3
answers1422
viewsA: Search for words that contain a certain letter
It may make sense to use regular expressions to potentially control the definition and limits of "word". "Existem no zoo: zebra, ...".split() --> ["zoo:", "zebra,"] I would write: import re…
-
0
votes2
answers765
viewsA: Typeerror: split() Missing 1 required positional argument: 'string'
By the way, always put the r of raw mode -- that is to say r'expressãoregular' -- for example '\s' r'\s' are different things... import re with open('est.txt', 'r') as f: for ligne in f: # para cada…
-
1
votes3
answers58
viewsA: I cannot convert mongodb files to json in python using bson in python
I have difficulty testing but bson has a direct conversion function. So it should be possible for you to do: import bson dados = colecao.find_one() d = beson.decode(dados) print(d) ## e d seria tipo…
-
1
votes3
answers214
viewsA: Calculate increments so that 2 numbers become equal
Just a small rewrite (of obfuscated and dubious taste) of the @hkotsubo version, in order to decompress a boring job I have to do... a,b = sorted(map(int,input().split())) q,r = divmod(b-a,3) print(…
-
2
votes2
answers98
viewsA: How do I know if a value is higher or lower than another value in a list using recursion?
By the way another version (this non-recursive). Considerarei que o monstro M1 é mais fraco que todos sse não há nenhum M2 tal que M1 possa atacar M2 -- M1.ataque > M2.defesa ou M1 consiga…
-
1
votes4
answers416
viewsA: How to remove multiple blank lines in sequence with vim?
Replace 2 or more blank lines (\n\n\n*) for two blank lines (\r\r) :%s/\n\n\n*/\r\r/ Note: in vim, substitutions are used \r instead of \n…
-
3
votes2
answers311
viewsA: How to use re.split() to pick only the words of a text, ignoring numbers and punctuation marks
For language processing, it is customary to start with tokenization (split text in its basic elements: words, scores, etc) -- broader problem than the request. In the example below follows a…
-
1
votes2
answers413
viewsA: How to copy files from one directory to another using regex?
By the way a couple more variants (in the @hksotsubo continuum): cp ~/Downloads/AB* ~/Downloads/HU* dir/ (that is to expand the alternative in some cases is an alternative) Although to a limited…
-
1
votes3
answers77
viewsA: Is there a bash iterator equivalent to the python enumerate?
By the way, another variant: i=0; for a in Harry Ron Voldermort ; do echo "$((++i)): $a"; done producing the expected 1: Harry 2: Ron 3: Voldermort…
-
1
votes4
answers1138
viewsA: Delete the last line of a txt file
If we happen to be on *Nix systems, a solution would be head -n -1 file > novo ... but does not use Python.
-
0
votes3
answers117
viewsA: How to identify output status in a bash program?
Try: Programa && echo "### ok" || echo "### not ok" Explanation: programa && echo "ok" se programa retorna 0: 0 && x = 0 (nada mais é executado) se programa retorna não 0:…
-
0
votes2
answers28
viewsA: How to identify the absence of a term in a file using PERL
By the way a slightly more cryptic version: print ( join(";", grep /target/,@report) || "not found\t") Where: grep /target/,@report gives the list of elements of the report that match; (Example:…
-
0
votes3
answers3650
viewsA: Scanf function with variable amount of parameters, how to implement?
In addition to the above, I have added a variant for the: each row has a variable number of integers, max. N (example N=4) we intend to read them, and know how many were we are taking advantage of…
-
0
votes5
answers312
viewsA: Make a function that calculates the following sum:
Using lists in comprehension (or iterators in comprehension) from math import factorial as fac print(sum((11-x)/fac(x)*(1 if x%2 else -1) for x in range(1,11)))
-
4
votes1
answer97
viewsA: Linux Shell Script - Difference between $(()) and (())
Paul, This is not an answer but a long comment In a simplistic way: $x ---> val(x) (( exp )) --> eval(exp) That is your example in slow motion: echo $((x)) :: echo val(eval(x)) ==> echo…
-
0
votes2
answers93
viewsA: How to print accents in Lua
It’s not really an answer... I suggest you install the moon 5.3 (which has better support for Unicode) In linux this runs perfectly. I do not use or know windows but: I suggest you get a good…
-
3
votes3
answers415
viewsA: Interfacing of vectors in python
Using lists in comprehension (namely [ x | par ∈ zip(a,b) ∧ x ∈ par ]): a=[4, -9, 78, 0, 25] b=[8, 2, 34, 90, 200] print([x for par in zip(a,b) for x in par]) Exit: [4, 8, -9, 2, 78, 34, 0, 90, 25,…
python-3.xanswered JJoao 5,113 -
1
votes2
answers674
viewsA: Treat Curl return on Linux
The ideal in this type of problem is to use tools that recognize the JSON format. Example with the jq $ S=$(jq -r .success <<< $RESULT) $ M=$(jq -r .message <<< $RESULT) $ echo $M…
-
1
votes4
answers200
viewsA: Find character succeeded by another character using Regex
As stated: 1: try to correct at source 2: try to fix using Try json.loads Using strictly regular expressions I would try to mark ONLY the occurrences of "] spaces [" keeping the formatting, not…
-
0
votes2
answers405
viewsA: Ignoring a line from a txt file
Already now a more compact version of the (and less readable) version of the @Giovanni reply (I’m a bit addicted to lists by understanding) def le(nome): with open(nome, "r") as arquivo: return[…
-
0
votes1
answer62
viewsA: Perl script - how to calculate frequencies based on the size of the sequences?
Instead of the map (which in this case is a bit confusing) he proposed: $len=scalar @dipeps; for(sort keys %di_count){ print " ",$di_count{$_}/$len} (untested)…
-
2
votes1
answer59
viewsA: Capture Numbers in File and Save Answer (Linux)
sed -r 's/.* ([0-9]{14}).*/\1/' exemplo > saida where: .* takes a string completed by space ([0-9]{14}) pick and hold (in 1) a sequence of 14 digits .* catch the rest of the line s/.../\1/ and…
-
0
votes4
answers87
viewsA: Comparing dictionary values() with list values
Only one variant of the @Claudinei version using lists in comprehension from collections import Counter lista = [ 21 ...... dic = { 1:68 ...... print([k for k,c in Counter(dic).most_common(10) if k…
-
3
votes2
answers65
views -
0
votes2
answers69
viewsA: I need a parser-building tool
For Python my favorite is the Lark-parser https://github.com/lark-parser/lark (Pip install Lark-parser).
-
0
votes1
answer53
viewsA: json manipulation via command line
(Test with the usual jq 'programa' f.json) Try something like selecting the ones that matter: .[] | select(.ssh_url_to_repo | contains("one/piece")).last_activity_at Another alternative would be…
-
1
votes2
answers454
viewsA: How to create a script to rename files on Ubuntu 18?
Normally under Linux there is (or can be installed) a command Name, or name that helps with these tasks: prename 's/nanatsu-ep-//' nanatsu-ep-*.mp4 That is to say: for all the nanatsu-ep-*.mp4…
-
0
votes3
answers227
viewsA: Add a string before a given word using Regex
In general, processing C with regular expressions can be complicated... For simple situations we can try something like text = ''' i=b+c2; a="uma e a outra"; /* ignorando o a,b, */ '''…
-
0
votes2
answers201
viewsA: switch case in bash
The @hkotsubo response looks great, together only the typical structure of a script bash with options and parameters, according to my personal tastes: #!/bin/bash while getopts "hi:" OPT ##…
-
0
votes3
answers102
views -
1
votes4
answers249
views -
1
votes4
answers295
viewsA: Print null or non-null in Python array
Like True > False we can simply calculate the minimum recursively: def all_0(x): if type(x) is list: return min([all_0(y) for y in x]) else : return x == 0 applicable to "matrices" of any size,…
-
0
votes3
answers665
viewsA: How to turn content from a file into a dictionary in Python?
Using Regular Expressions (import re): import re v=open("f.ini").read() print(dict(re.findall(r'(\w+)=(.*)',v)))
-
0
votes2
answers523
viewsA: Eval vs Ast.literal_eval: what are the differences?
This is not really an answer but an opinion (politically incorrect). Exchange formats Both json, yaml, xml, ... modules contain functions that recognize the respective formats (languages) and create…
-
0
votes4
answers232
viewsA: Transform a numeric string into a list of python numbers
a = ['[11, 14]', '[8, 17, 18, 24, 29, 37, 44, 49, 51, 55, 82, 84, 93, 97]'] Using lists in understanding we can: b=[eval(x) for x in a] ## [[11, 14], [8, 17, 18, 2...]] To get the single list we…
-
0
votes2
answers81
viewsA: Error calling function
By the way, a suggestion: make functions a little more generic. Example - a function that gives the bit value n of a number: (Python3) def bit(n,input): n=n-1 return (input & 2**n) // 2**n…
-
0
votes2
answers126
viewsA: Python Pandas - How to check if a "tuple" of two Series elements is contained in a set?
Using zip and lists in understanding: z=zip(......) df['three'] = [x in set for x in z]
-
2
votes3
answers482
viewsA: Rescue only 10 first Python object records
If your Python is recent, try using lists in comprehension and f-strings: tab=[f'Nome: {x.getNome()}; {x.getRA()}' for x in alunos[:10]]
-
3
votes3
answers840
viewsA: Replace word list in text
import re txt = open('texto').read() lista= open('lista').read() sw = re.findall('\w+',lista) print(re.sub('\w+', lambda x: '' if x[0].lower() in sw else x[0] ,txt)) Here’s a Python3 variant:…
-
0
votes2
answers3159
viewsA: Python Taylor series with no angle and error margin
Your duties sen(x) = x/1! – x3/3! + x5/5! - ... + (-1)k . x2k+1/(2k+1)! + ... cos(x) = 1 – x2/2! + x4/4! – x6/6! + ... + (-1)k . x2k/(2k)! + ... ex(x) = 1 + x + x2/2! + x3/3! + x4/4! + ... + xn/n! +…
-
2
votes3
answers1433
viewsA: How to create list in Haskell
The most direct in Haskell is the operator !! lista !! n To prevent miscarriage n is out of bounds: nesimo :: Int -> [a] -> Maybe a nesimo _ [] = Nothing nesimo 1 (x : _) = Just x nesimo n (_…
-
0
votes3
answers2068
viewsA: PYTHON- Program that prints on screen all numbers divisible by 7 but not multiples of 5, between 3000 and 3200 (inclusive)
print([i for i in range(3003,3201,7) if i%5 != 0]) where 3003 is the first multiple of 7 in that range, range(3003,32001,7) - runs every 7…
python-3.xanswered JJoao 5,113 -
0
votes1
answer5794
viewsA: List of Acronyms latex
\usepackage[nopostdot,toc,acronym,nomain]{glossaries} \makeglossaries \setacronymstyle{long-short} \loadglsentries[acronym]{example-glossaries-acronym} \begin{document}…
-
0
votes2
answers547
viewsA: Image and text in Latex
Tries: \begin{figure}[!h] ... \end{figure} but the result is a bit ugly... Alternatively: \begin{figure}[!htb] ... \end{figure}
-
3
votes3
answers10243
viewsA: Insert author and image description. Latex
In addition to @Willian’s elegant solution, I remind you that you can write Latex inside of the environment figure, with the content you want... \documentclass{article} \usepackage[brazilian]{babel}…
-
0
votes1
answer50
viewsA: Insert perl timer
Miguel, If I understand correctly, you intend to take breaks, (not to saturate the system?). If so, try something like putting a little sleep(x) in each shipment ( x in seconds), #...ciclo de envio{…
-
8
votes5
answers453
viewsA: How to separate "words" in Camelcase in C#?
Using regular expressions we can for example: using System.Text.RegularExpressions; ... id = Regex.Replace(id,"(?<=[a-z])(?=[A-Z])","-"); A complete example could be: using static System.Console;…
-
6
votes2
answers1556
viewsA: Regular expression 6 decimal places
It is not the ideal way, but having to be: ^((3[1-9]|4[0-2])(\.\d{0,6})?|43(\.0{0,6})?)$