Reading line by line from text file and assign each value into a specific variable
I have to build an automated test scenario where I need to populate multiple fields in a web page. The data to populate the specific fields come from a text file
in1=data1,in2=data2,in3=data3
in11=data11,in12=data12,in13=data13
in21=data21,in22=data22,in23=data23
where each key/value pair is deliminated by a comma.
I need to read this file, line by line, and assign each key/value pair to its unique variable. Once the key/value pairs have been assigned to a unique variable I will pass those variables as parameters to a python script that uses the parameters to build a dictionary object and hands it to a http request.
I have completed a python script that uses a python array instead of a text file to read the key/value pairs from and have successfully passed that to the http request and successfully filled the fields in the web page.
However, I need to be able to read the data from the bash script so that I can send the values to the python script
So far, in the bash script, I can read from the file and, using awk, print each key/value pair to the screen. However, I need to be able to assign each key/value pair to a variable as I read the text file line by line.
In the end I should have for line one:
var1 = in1, var2 = data1, var3 = in2, var4 = data2, var5 = in3, var6 = data3
Now, line one is passed to the python script
%python python_script.py $var1 $var2 $var3 $var4 $var5 $var6
Then, read next line from text file and assign key/value pair to unique variable and send the variable开发者_如何学JAVAs to the python script.
while read line
do
echo $line | awk (Could not figure out the arguments for awk)
python python_script.py [parameter list]
done < input_file.txt
f = open('/tmp/workfile', 'w')
f.readline()
Is there a constraint that keeps you from using python's standard file object methods? http://docs.python.org/library/stdtypes.html#file-objects
with open("hello.txt") as f:
for line in f:
print line //here you can tokenize the line using an explode on comma
Okay, hold onto your seats. I'm going to make the following assumptions:
in1=data1,in2=data2,in3=data3
in11=data11,in12=data12,in13=data13
in21=data21,in22=data22,in23=data23
Your data is EXACTLY like this, with each line containing only 3 values comma separated
Now then, with regards to your requirements:
In the end I should have for line one:
var1 = in1, var2 = data1, var3 = in2, var4 = data2, var5 = in3, var6 = data3
Now, line one is passed to the python script
%python python_script.py $var1 $var2 $var3 $var4 $var5 $var6
Okay so here's the full code listing:
while read line
do
# split into pairs using cut
pair1=$(echo $line | cut -d ',' -f 1)
pair2=$(echo $line | cut -d ',' -f 2)
pair3=$(echo $line | cut -d ',' -f 3)
# split again into key/values using cut
key1=$(echo $pair1 | cut -d '=' -f 1)
value1=$(echo $pair1 | cut -d '=' -f 2)
key2=$(echo $pair2 | cut -d '=' -f 1)
value2=$(echo $pair2 | cut -d '=' -f 2)
key3=$(echo $pair3 | cut -d '=' -f 1)
value3=$(echo $pair3 | cut -d '=' -f 2)
params="$key1 $value1 $key2 $value2 $key3 $value3"
python python_script.py $params
done < "file.txt"
Please respond in the comments if the assumptions or incorrect or if anything needs explaining.
I'm not sure than understand what you need, but this:
< input_file.txt sed 's/[^A-Za-z0-9]/ /g' | while read line
do
python python_script.py $line
done
will run your python_script.py
as next:
python python_script.py in1 data1 in2 data2 in3 data3
python python_script.py in11 data11 in12 data12 in13 data13
python python_script.py in21 data21 in22 data22 in23 data23
this is ok?
ps: dont worry, your python got 6 parameters, not only 1 :)
I realize this is a late answer... but this might be useful to a future visitor:
#!/bin/bash
argCounter=0
while IFS= read -r line
do
#echo "line is " $line
IFS=, read -a kvPairs <<< "$line"
#echo "there are " ${#kvPairs[@]} " kv pairs in this line"
for kvp in "${kvPairs[@]}"; do
#echo "kvp = " $kvp
IFS== read key value <<< "$kvp"
valVec[$argCounter]=$key
((argCounter++))
valVec[$argCounter]=$value
((argCounter++))
#echo "key: " $key "; value: " $value "; counter: " $argCounter
done
IFS=
done <$1
printf "python myScript "
for x in "${valVec[@]}"; do
printf "%s " $x
done
printf "\n"
It makes no assumptions about the size of each line; it takes the entire input ($1
), reads it one line at a time, then splits that line on commas.
The result of that split is fed to an inner loop that creates key/value pairs, and it appends these to an array. There are a couple of commented-out echo
statements that can be used to monitor what is going on.
Finally, that array is printed out. You may have other ways to use this.
For the given input file, the output is
python myScript in1 data1 in2 data2 in3 data3 in11 data11 in12 data12 in13 data13 in21 data21 in22 data22 in23 data23
Which I believe is what was asked for.
精彩评论