Feb-25-2019, 08:43 AM
Pages: 1 2
Feb-25-2019, 03:55 PM
I explained what you should change and where you should change it. It's time for you to try and implement it, and come back to me with any problems you have.
Feb-26-2019, 07:57 AM
I tried this one and I got it. But I can not figure out to write the result to a file. Any idea?
FBlist_set = set(FBlist) Array_set = set (Array) if Array_set & FBlist_set: print ("found") result.write("found") #?????? else: print ("Not Found")
Feb-26-2019, 04:42 PM
If you still want to write out 1's and 0's, you would change line 6 to
result.write('1')
, and add result.write('0')
under the else statement.Feb-27-2019, 02:31 AM
It returns an error with this code
ValueError: I/O operation on closed file.
if Array_set & FBlist_set: print ("Found") result.write("1") else: print ("Not Found") result.write("0")The error is : Traceback (most recent call last):
ValueError: I/O operation on closed file.
Feb-27-2019, 04:23 AM
I can't diagnose that from the current snippet. Please show your current full code.
Feb-27-2019, 06:54 AM
This is all my current code :
infile = "test.txt" outfile = "Result.txt" Array = ["6J", "yB", "ss", "11"] with open(infile, "r") as input_file: with open(outfile, "w") as result: output_list = [] for rec in input_file.read().splitlines(): rec = rec[:-3] FBlist = [rec[i:i+2] for i in range(0, len(rec), 2)] output_list.append(FBlist) print(output_list) FBlist_set = set(FBlist) Array_set = set (Array) if Array_set & FBlist_set: print ("Found") result.write("1") else: print ("Not Found") result.write("0")
Feb-27-2019, 04:16 PM
You need to indent everything from line 14 on twice. When the with clause on line 6 ends (after the indented block below it), the file associated with 'result' closes, and you can't access it anymore. So you need to indent the rest of the code to be under that with clause.
Pages: 1 2