perl

Python write bytes to file using redirect of print

Python write bytes to file using redirect of print Question: using perl, $ perl -e ‘print "xca"’ > out now $ xxd out we have 00000000: ca But with Python, I tried $ python3 -c ‘print("xca", end="")’ > out $ xxd out what I got is 00000000: c38a I’m not sure what is going on. …

Total answers: 2

Rearranging a list to get the 2nd column entries as rows

Rearranging a list to get the 2nd column entries as rows Question: I have a list associated to strings as follows; A string1^description1`string2^description2`string3^description3 B string4^description4 C string1^description1`string5^description5`string3^description3 D . E string6^description6`string1^description1 F string7^description7 G string1^description1`string4^description4`string5^description5 I would like to switch the first and second columns so that the stings in the 2nd column are the …

Total answers: 3

Why is this regular expression so slow in Java?

Why is this regular expression so slow in Java? Question: I recently had a SonarQube rule (https://rules.sonarsource.com/java/RSPEC-4784) bring to my attention some performance issues which could be used as a denial of service against a Java regular expression implementation. Indeed, the following Java test shows how slow the wrong regular expression can be: import org.junit.Test; …

Total answers: 4

How to invert the regular expression group capture logic?

How to invert the regular expression group capture logic? Question: To create a capturing group in a regex you use (match) and you prefix it with ?: to make it non-capturing, like (?:match). The thing is, in any kind of complicated regular expression I find myself wanting to create far more non-capturing groups than capturing …

Total answers: 2

Replace combinations of characters

Replace combinations of characters Question: I have a string 27AAGCB5913L2ZF. If any of A or J or K appear in the string then it I need to change them to all possible combinations of the three letters. If I pass the above string input to the program then the output should be like this 27AAGCB5913L2ZF …

Total answers: 4

Is there are Python equivalent of Perl's __DATA__ filehandle?

Is there are Python equivalent of Perl's __DATA__ filehandle? Question: In Perl I often read data in from the filehandle __DATA__ at the end of the script: while (<DATA>) { chomp; say; } __DATA__ line1 line2 I find this quicker for testing code etc than reading in a file, as it means I can edit …

Total answers: 2

Split large directory into subdirectories

Split large directory into subdirectories Question: I have a directory with about 2.5 million files and is over 70 GB. I want to split this into subdirectories, each with 1000 files in them. Here’s the command I’ve tried using: i=0; for f in *; do d=dir_$(printf %03d $((i/1000+1))); mkdir -p $d; mv “$f” $d; let …

Total answers: 6

Passing command line argument to subprocess module

Passing command line argument to subprocess module Question: I have a Python script which calls Perl script using subprocess module. In terminal I run the Perl script like this: perl email.pl [email protected] I am passing in "[email protected]" as command line argument to that script. This is my Python script: import subprocess pipe = subprocess.Popen(["perl","./email.pl"]) print …

Total answers: 1

Why don't I get any syntax errors when I execute my Python script with Perl?

Why don't I get any syntax errors when I execute my Python script with Perl? Question: I just wrote some testing python code into test.py, and I’m launching it as follows: perl test.py After a while I realized my mistake. I say “after a while”, because the Python code gets actually correctly executed, as if …

Total answers: 1