File reading from one to another file and printing in Python - python

The following code is working as expected. As I have taken InputStr.txt in that I have filled some string of statements and generating the Output.txt by adding those InputStr.txt statements whether PASS or FAIL.
Python Code:
str = "FAIL";
flag = False;
resultList = [];
#Here InputStr.txt is the file where all your output format strings are stored.
Stringlines = [line.strip() for line in open('InputStr.txt')]
out_file = open("Output.txt", "w")
for i in range (1,3):
fileName1 = "./largeFile%d.txt"%i
print fileName1;
fileName2 = "./largeFile_%d.txt"%i
with open(fileName1, 'r') as inF1:
for line in inF1:
if 'VERSION' in line:
print "VERSION FOUND";
flag = True;
if flag:
with open(fileName2, 'r') as inF2:
for line in inF2:
if 'ID' in line:
print "ID FOUND";
str = "PASS";
resultList.append(str);
out_file.write("\n %s - %s" %(Stringlines[i-1],resultList[i-1]))
out_file.close()
Output I got:
Output in Output.txt is:*
TEST CASE-1 PASS
TEST CASE-2 PASS
TEST CASE-3 PASS
I am trying to print the TC1 should PASS when 4TCs should PASS as these 4TCs i took from another InputStr2.txt.
Example:
InputStr.txt contains following statements:
Req 1. How How are you
Req 2. I am Fine and Thanks.
Req 3. How are you doing
InputStr2.txt contains:
TC1: ABCDEFGHIJ
TC2: KLMNOPQRST
TC3: UVWXYZABCD
TC4: EFGHIJKLMN
So expected output is:
Req 1. How How are you - PASS
Req 2. I am Fine and Thanks. - PASS
Req 3. How are you doing - PASS
First All the Test cases of TC1 to TC4 should PASS(In InputStr2.txt), then only my Req 1 will PASS. Means, In Output.txt i am printing all the requirements are PASS or FAIL.
Logic/suggestions please?

Related

Try except block python varies result

I am reading some .json files. Some of the files are empty. So I am using try-except block to read.
If it is empty then except condition will be executed. My code snippet looks like this:
exec = True
for jsonidx, jsonpath in enumerate(jsonList):
print("\n")
print(">>> Reading {} of {} json file: {}".format(jsonidx, len(jsonList), os.path.basename(jsonpath)))
try:
with open(jsonpath, "r") as read_file:
jsondata = json.load(read_file)
outPath = r'{}'.format(outPath)
dicomPath = os.path.join(outPath, 'dicoms')
nrrdPath = os.path.join(outPath, 'nrrds')
if exec: # if you want to execute the move
if not os.path.isdir(outPath): # if the outPath directory doesn't exist.
os.mkdir(outPath)
os.mkdir(dicomPath)
os.mkdir(nrrdPath)
thisJsonDcm = []
for widx, jw in enumerate(jsondata['workItemList']):
# print('\n')
print('-------------------- Extracting workitem #{} --------------------'.format(widx))
seriesName = jw['imageSeriesSet'][0]['seriesLocalFolderName'] # this is dicom folder whole path
thisJsonDcm.append(seriesName)
except:
print("Json empty")
The code ran perfectly at the first couple of times or so, where it iterates the second for loop with jsondata["workItemList"].
But when I run later again, the second for loop doesn't iterate and all the iterations show the print statement inside except json empty.
Does try-except block have any state or specific behavior?? Do I need to delete or refresh something after running the first time to repeat again?
All you need is json.decoder.JSONDecodeError exception.
It looks like this:
try:
pass
"""Some stuff with json..."""
except json.decoder.JSONDecodeError:
print("Json empty")
More about in Json Docs
Or you can handle error only when loading json
exec = True
for jsonidx, jsonpath in enumerate(jsonList):
print("\n")
print(">>> Reading {} of {} json file: {}".format(jsonidx, len(jsonList), os.path.basename(jsonpath)))
with open(jsonpath, "r") as read_file:
try:
jsondata = json.load(read_file)
except json.decoder.JSONDecodeError:
print("Json empty")
continue
outPath = r'{}'.format(outPath)
dicomPath = os.path.join(outPath, 'dicoms')
nrrdPath = os.path.join(outPath, 'nrrds')
if exec: # if you want to execute the move
if not os.path.isdir(outPath): # if the outPath directory doesn't exist.
os.mkdir(outPath)
os.mkdir(dicomPath)
os.mkdir(nrrdPath)
thisJsonDcm = []
for widx, jw in enumerate(jsondata['workItemList']):
# print('\n')
print('-------------------- Extracting workitem #{} --------------------'.format(widx))
seriesName = jw['imageSeriesSet'][0]['seriesLocalFolderName'] # this is dicom folder whole path
thisJsonDcm.append(seriesName)
You can read more about try/except in Python Documentation

python does not read the second line of the file

i have a text file with the below contents
url1,user1,xxxxxxxxx
url2,user2,yyyyyyyyy
I have a block of code that is supposed to get the value xxxxxxxxx or yyyyyyyyy based on the env value passed(prod or test)
#!/usr/bin/python
import os
class test:
def __init__(self, env):
self.env = env
def func(self):
res = []
try:
if os.path.exists("file.txt"):
try:
with open("file.txt", 'r') as fp:
for line in fp:
print("line is " +line)
line_api = line.split(',')[2]
print(line_api)
res.append(line_api)
print(res)
if self.env == "prod":
self.api = res[0]
print(self.api)
else:
self.api = res[1]
print(self.api)
except Exception as e:
print(e)
except Exception as e:
print(e)
value when else part is executed
list index out of range
Now when the env passed is prod the function works but when the value is test and the else part is executed the value of list res is only xxxxxx, there is only one value in list and the code self.api = res[1] fails. print(res) only prints ['xxxxxxxxxxxx\n'] but when url passed is url1 print(res) only prints both ['xxxxxxxxxxx\n', 'yyyyyyyyy \n']
What is wrong with my code?
The issue with your code is that you split each line that you read in with line_api = line.split(',')[2] and the [2] is referencing the third element that exists in that list which is xxxxxxxxx or yyyyyyyyy, then when you call self.api = res[0] you reference the first (and only) element in that list.
self.api = res[1] will always throw an error because it will never exist in the 1 element list. I'm not sure what the goal was for this else statement, but I would suggest using DirtyBit's elegant solution
Since it is hard to debug your already excessive code, Here is a shorter snippet:
Using startswith():
list.txt:
url1,user1,xxxxxxxxx
url2,user2,yyyyyyyyy
Hence:
logFile = "list.txt"
def getUrlValue(url):
with open(logFile) as f:
content = f.readlines()
# you may also want to remove empty lines
content = [l.strip() for l in content if l.strip()]
for line in content:
if line.startswith(url):
print(line.split(',')[2])
getUrlValue("url1")
getUrlValue("url2")
OUTPUT:
xxxxxxxxx
yyyyyyyyy

If statement search for string returns false but printed output displays string

I'm using this function to search a site to see if a particular item i'm interested in s on sale. It first grabs the html from the page, then searches for an item i'm interested. When it finds the item it adds a number of the following lines (dictated by the rangenumber) to the variable 'endresult'. It then searches for the keyword ("sale") in endresult, at which point I'd like it to notify me if the keyword is present or not.
When I print endresult the output contains the keyword, but the if statement at the very end of the function always returns "keyword is missing" despite this and I can't work out why.
def bargainscraper(self, website, item, keyword,rangenum):
request = urllib.request.Request(website)
response = urllib.request.urlopen(request)
data = response.read()
html = str(data)
data1 = html2text.html2text(html)
fw = open('result1.txt', 'w')
fw.write(str(data1))
fw.close()
with open('result1.txt', 'r') as f:
for line in f:
if item in line:
for x in range(rangenum):
endresult = str(f.readline())
print (endresult)
if keyword in endresult:
print("keyword is present")
else:
print("keyword is missing")
Possibly need to concatenate the endresult instead of overwriting it with something like: endresult += str(f.readline()) notice the "+" before the "=".
I found writing the endresult to a file within the for loop then searching that file for the keyword outside of the for loop was the answer I was looking for:
def bargainscraper(self, website, item, keyword,rangenum):
request = urllib.request.Request(website)
response = urllib.request.urlopen(request)
data = response.read()
html = str(data)
data1 = html2text.html2text(html)
fw = open('result1.txt', 'w')
fw.write(str(data1))
fw.close()
with open('result1.txt', 'r') as f:
for line in f:
if item in line:
for x in range(rangenum):
endresult = str(f.readline())
# the 'a' switch is used to append
with open('result2.txt', 'a') as n:
n.write(endresult)
# This is outside of the for loop as otherwise it will iterate for each line of the rangenum
if keyword in open('result2.txt').read():
print ("keyword is present")
else:
print ("keyword is missing")

python: search file for string

I have tried to create a python function which takes in 2 parameters; a file name and a search string. In this case the file name is the script itself (script.py) and the search string is 'name = "JOHN"'
#!/usr/local/bin/python2.7
import os, sys
#################
# Variable string
name = "JOHN"
#################
# Main function
def search_script_for_string(filename, searchString):
f = open(filename,'r') #open the given filename then
filedata = f.read() #assign it to variable then
f.close() #close the open filename
for lines in filedata: #loop through each line in the filedata variable
if searchString in lines: #if search string is found, then do all of this
print ('Found string: %s') % searchString
return True
else: #if not found, then do all of this
print ('Did not find: %s') % searchString
return False
break
#################
# Pass the file name and the search string parameter to the function
search_script_for_string("test.py","name = \"" + name + "\"")
The problem is that it doesn't return expected results:
$ Did not find: name = "JOHN"
When it meant to say:
$ Found string: name = "JOHN"
If anyone can help correct my understanding of where I'm going wrong here, I'd be massively appreciative. Thanks
f.read() returns the entire contents of the file as a single string. You then iterate over those contents -- but iterating over a string yields only 1 character at a time so there is no way a character will contain the substring you are looking for.
def search_script_for_string(filename, searchString):
with open(filename, 'r') as f:
return searchString in f.read()
should do the trick. Alternatively, if you want to search line-by-line:
def search_script_for_string(filename, searchString):
with open(filename, 'r') as f:
for line in f:
return searchString in line
You are iterating over each character of the file by calling for c in f.read().
Use for line in f and you will indeed iterate over each line.
Also prefer the use of with, this makes your code a lot more robust.
So this would be better:
with open('fileName') as f:
for line in f:
#process

Dictionary seems ok , but function returns "List index out of range"

I'm not sure why this function is returning "List index out of range" Since it seens ok. What the function does is basically work the content of the file.txt to create a dictionary with the values.
Resuming, the function will transform the content of the file.txt:
access_1;test_group_1,test_group_2
to:
[{'groups': 'test_group_1,test_group_2\n', 'acl': 'access_1'}
The current code:
def prepare_list():
try:
input_list = []
ins = open("file.txt", "r")
for line in ins:
values = dict()
values['acl'] = line.split(";")[0]
values['groups'] = line.split(";")[1]
input_list.append(values)
except Exception, e:
print "Error : %s" % e
finally:
return input_list
Output:
Error : list index out of range
Process finished with exit code 0
I'm running the script on the folder where the file exists.
data = "a;b"
print data.split(";") # Prints ['a', 'b']
data = "ab"
print data.split(";") # Prints ['ab']
print data.split(";")[1] # raises IndexError: list index out of range
So, if ; is not there, it will return only one element. And if you try to access the second element, it will fail with that error.
If you know for sure that, all lines will have ; in them, your whole program can be written like this
keys = ("acl", "groups")
with open("Input.txt", "r") as ins:
input_list = [{k:v for k, v in zip(keys,l.rstrip().split(";"))} for l in ins]
print input_list
Based on the answer given by thefourtheye I was able to solve the problem testing if the file have lines with ; and striping new lines:
I could use the following correction:
def prepare_list():
try:
input_list = []
ins = open("file.txt", "r")
for line in ins:
if line.strip() and ';' in line:
values = dict()
values['acl'] = line.split(";")[0]
values['groups'] = line.split(";")[1]
input_list.append(values)
print "Correct : ",line
else:
print "line not correct. please fix it : ", line
return False
except Exception, e:
print "Error : %s" % e
But it would print " line not correct, please fix it " for each new line, example with file with 4 new lines:
line not correct
line not correct
line not correct
line not correct
So, using the following i can remove the new lines, and clean the output to just show what is really wrong:
file content:
access_1;test_group_1,test_group_2
access_2test_group_1,test_group_2
<new line>
<new line>
<new line>
<new line>
function:
def prepare_list():
try:
input_list = []
ins = open("file.txt", "r")
for line in ins:
# go away new lines, GO AWAY
if line.strip()
# test for valid char in line
if ';' in line:
values = dict()
values['acl'] = line.split(";")[0]
values['groups'] = line.split(";")[1]
input_list.append(values)
print "Correct : ",line
else:
print "line not correct. please fix it : ", line
except Exception, e:
print "Error : %s" % e
output:
Correct : access_1;test_group_1,test_group_2
line not correct. please fix it : access_2test_group_1,test_group_2
Thanks for the help thefourtheye :)

Categories

Resources