Feb-25-2019, 08:43 AM
Could you please explain it what should I change? and what should it be? THanks
How to check an array exist in a file using Python
|
Feb-25-2019, 08:43 AM
Could you please explain it what should I change? and what should it be? THanks
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness. Recommended Tutorials: BBCode, functions, classes, text adventures
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness. Recommended Tutorials: BBCode, functions, classes, text adventures
Feb-27-2019, 02:31 AM
It returns an error with this code
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness. Recommended Tutorials: BBCode, functions, classes, text adventures
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness. Recommended Tutorials: BBCode, functions, classes, text adventures |
|
Possibly Related Threads… | |||||
Thread | Author | Replies | Views | Last Post | |
Newbie here. Create an array from file data? | Rayj00 | 2 | 1,847 |
Jan-13-2023, 01:35 PM Last Post: perfringo |
|
how to check if file path finish on .csv | danlopek14q | 4 | 13,271 |
Apr-04-2021, 09:50 AM Last Post: danlopek14q |