parsing subprocess stdout values [closed] - python

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
The stdout of given command is in form of iterable string but not printing actual values.
import subprocess
subnets = subprocess.run('''aws ec2 describe-subnets | jq -r '.Subnets[] | select(.Tags[].Value == "az1") .SubnetId' ''', stdout=subprocess.PIPE, shell=True)
print(subnets.stdout.decode('utf-8'))
The stdout is
subnet-01234567
subnet-abcdefgh
When I am iterating over the stdout, it's iterating in string iterable fashion ex ['s','u','b','n','e'...] but not actual values ex "subnet-01234567, subnet-abcdefgh".
How do I fix it!

If you want to iterate over the values printed in separate lines I suggest that you separate by new line ('\n'):
str_out = subnets.stdout.decode('utf-8')
for part in str_out.split('\n'):
print(part)

Related

read .conf file python ConfigParser [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 2 years ago.
Improve this question
I need to read config.conf file in Python using ConfigParser. The thing I'm trying to do is, club $dataDir variable with a fileName and can't get expected output as given below.
config.conf
[dir]
dataDir = /home/srujan/Documents/r2d2/data-cut/SLIM/postcut
[files]
current_materiel = ${{dataDir}} + /20201209-rtvm_api_current_materiel_not_filtered_OS.csv
Expected output:
/home/srujan/Documents/r2d2/data-cut/SLIM/postcut/20201209-rtvm_api_current_materiel_not_filtered_OS.csv
Debug results - I get as a text instead of dataDir path.
${{dataDir}} + /20201209-rtvm_api_current_materiel_not_filtered_OS.csv
You are looking for https://docs.python.org/3/library/configparser.html#interpolation-of-values.
The default interpolation only allows use of variables from the same section with %(varname) syntax. If you want variables to persist across multiple sections, you need to use the extended interpolation
parser = configparser.ConfigParser(..., interpolation=configparser.ExtendedInterpolation())
Extended interpolation uses ${section:option} to denote a value from a foreign section.

No "\n" after line.rsplit in python [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 3 years ago.
Improve this question
I want to create a new text file and cut the string in each line.
for line in rescut:
rescutfinal.write("pretext_" + line.rsplit('delimiter', 1)[-1] + "1\t0\t10\tLinear\t0\t0")
But my code doesn't work as expected. My Outpout contains a new line after the "line.rsplit"-string
pretext_linesplitstring
string after linesplitstring pretext_linesplitstring
string after linesplitstring pretext_linesplitstring
string after linesplitstring pretext_linesplitstring"
How do I get rid of the "\n" after the linesplitstring?
you can use str.rstrip() or str.strip():
line.rstrip().rsplit('delimiter', 1)[-1]

Convert the output of ls in a python list [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 3 years ago.
Improve this question
I get the listing of a directory using bash command ls(can't use python on remote)
How can I convert the return of ls from remote in a python list/set containing filenames?
you can just use os.listdir, like this:
import os
files = os.listdir()
EDIT: if you have any control over the ls cmd, you can add the -1 flag, to get one filename per line, then use .splitlines() to turn it into a list.
regular split on the regular ls would fail on filenames that contain spaces.

Python print to file function is not printing to file [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 3 years ago.
Improve this question
I am using the print function with output to file argument. The print function is under an if statement. Below is the code
log_file = open("Src_files.log", 'w')
if count_1.equals(count_2) == False:
print('Error: Discrepancy with processed file. Count of records does not match with sources file', file=log_file)
Count_1 and count_2 are unequal dataframes
The code gets executed without throwing any error but when I check the log file, it does not contain the printed statement.
How do I correct the code?
print does not flush by default. check the python manual to find that there is a flush keyword arg, or simply close the file. log_file.close()

How to Empty Lines? [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 8 years ago.
Improve this question
I ran into a problem while trying to put lines from a .txt file into a list. I know you get extra lines when you do this, so I used line.split() to take out the trailing lines.
When I did this, the words I was trying to read became weirdly formatted. This is what it looked like...
['word']
Do any of you know how to take out the trailing lines without having this happen?
Just read all of lines with readlines() function and then you can get the n last line with reverse indexing : lines[-n:] and as says in comment its better to call the built-in list of open file handle !
with open('test_file.txt','r') as f :
lines =list(f)
or
with open('test_file.txt','r') as f :
lines =f.readlines()

Categories

Resources