Python Forum
How to check an array exist in a file using Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to check an array exist in a file using Python
#11
Could you please explain it what should I change? and what should it be? THanks
Reply
#12
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
Reply
#13
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")
Reply
#14
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
Reply
#15
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.
Reply
#16
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
Reply
#17
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")
Reply
#18
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Newbie here. Create an array from file data? Rayj00 2 1,198 Jan-13-2023, 01:35 PM
Last Post: perfringo
  how to check if file path finish on .csv danlopek14q 4 10,791 Apr-04-2021, 09:50 AM
Last Post: danlopek14q

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020