Read text file values and assign it to each value to variable
Clash Royale CLAN TAG#URR8PPP
Read text file values and assign it to each value to variable
I want to read text file and find the words that starts with 56 in below text and pass each word to a variable and pass to a python file as a parameters.
My sample text file content -
51:40:2e:c0:01:c9:53:e8
56:c9:ce:90:4d:77:c6:03
56:c9:ce:90:4d:77:c6:07
51:40:2e:c0:01:c9:54:80
56:c9:ce:90:12:b4:19:01
56:c9:ce:90:12:b4:19:03
I like to pass to python file as
mytestfile.py var1 var2 var3
var1 should have value 56:c9:ce:90:4d:77:c6:03
var2 should have value 56:c9:ce:90:4d:77:c6:07
var3 should have value 56:c9:ce:90:12:b4:19:01
so on
I wrote code something like below but not working
[#var1 = "51:40:2e:c0:01:c9:53:e8"]
[#var2 = "51:40:2e:c0:01:c9:53:ea"]
filepath = '/root/SDFlex/work/cookbooks/ilorest/files/file.txt'
with open(filepath) as fp:
line = fp.readline()
cnt = 1
while line:
print("Line : ".format(cnt, line.strip()))
line = fp.readline()
cnt += 1
execute "run create volume script" do
command "python SDFlexnimblevolcreate.py #var1 #node['ilorest']['Test0'] #var2 #node['ilorest']['Test1']
cwd "#platformdirectory"
live_stream true
end
Thanks in advance
mytestfile.py
I mean as a variable like mytestfile.py var1 var2 var3 var4. I want to assign it to variable that should increase for each value
– Ram Krishna
Aug 13 at 2:34
2 Answers
2
So this code prints string of required variables:
# a.py
with open('file.txt') as f:
result = ' '.join(map(lambda x: f'<x>',
filter(lambda x: x.startswith('56'),
map(str.strip, f))))
print(result, end='')
Output:
<56:c9:ce:90:4d:77:c6:03> <56:c9:ce:90:4d:77:c6:07> <56:c9:ce:90:12:b4:19:01> <56:c9:ce:90:12:b4:19:03>
You can pass result of this program using xargs
to some another python program. For example if you have simple program which prints their arguments:
xargs
# b.py
import sys
print(sys.argv)
Than just type in shell:
python3 a.py | xargs python3 b.py
Output:
['b.py', '<56:c9:ce:90:4d:77:c6:03> <56:c9:ce:90:4d:77:c6:07> <56:c9:ce:90:12:b4:19:01> <56:c9:ce:90:12:b4:19:03>']
how to do that using pipe?
– Ram Krishna
Aug 13 at 2:45
Please, read this wiki. You can pass the result of this script to
mytestfile.py
using pipeline. I will add an example soon.– Lev Zakharov
Aug 13 at 2:47
mytestfile.py
@RamKrishna updated!
– Lev Zakharov
Aug 13 at 2:55
But the OP deosn't want a pipe, they want the output to be passed as command-line arguments. (This is probably misdirected, though.) The solution to that is
xargs
, or you can run the second program as a subprocess from the first.– tripleee
Aug 13 at 3:33
xargs
Still probably not right, take out the
-0
(or actually print null-separated output from the first script).– tripleee
Aug 13 at 3:45
-0
Are you trying to run your mytestfile.py
once for each line that starts with '56:', like this:
mytestfile.py
python mytestfile.py 56:c9:ce:90:4d:77:c6:03
python mytestfile.py 56:c9:ce:90:4d:77:c6:07
python mytestfile.py 56:c9:ce:90:12:b4:19:01
Or do you want to run it one time only, giving it all values from the file as arguments, e.g., with your example data:
python mytestfile.py 56:c9:ce:90:4d:77:c6:03 56:c9:ce:90:4d:77:c6:07 56:c9:ce:90:12:b4:19:01
If it is the former (one run per matching line), the easiest way is:
grep '^56' textfile | xargs python mytestfile.py
otherwise, you could do this:
python mytestfile.py `grep '^56' textfile`
note the latter is dependent on having no spaces in the file - and is a bit dangerous if the file is very large (you could run into the command-line length limit).
I want to run python mytestfile.py 56:c9:ce:90:4d:77:c6:03 56:c9:ce:90:4d:77:c6:07 56:c9:ce:90:12:b4:19:01
– Ram Krishna
Aug 13 at 6:36
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Are you sure that you need to pass parameters for
mytestfile.py
in a such way?– Lev Zakharov
Aug 13 at 2:32