Bottom Page

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
Quote
#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

Quote
#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")
Quote
#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

Quote
#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.
Quote
#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

Quote
#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")
Quote
#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

Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  sending arduino data to python and use a parity check 10_Lucas 6 248 Aug-03-2019, 10:37 AM
Last Post: DeaD_EyE
  Please check code Evgeniy2019 4 272 Apr-05-2019, 06:37 PM
Last Post: Evgeniy2019
  Recursive Function - Compare 2 lists, return the elements that don't exist in both KellyBaptist 1 409 Dec-23-2018, 10:10 AM
Last Post: Gribouillis
  Hi how to take row 1 for every column and check the value what class is it kirito85 2 477 Nov-21-2018, 06:52 AM
Last Post: kirito85
  Check if string is uppercase or lowercase and eliminate Wolfpack2605 1 1,523 Jan-01-2018, 05:03 AM
Last Post: Mekire
  Check if integer is between two values Wolfpack2605 3 14,782 Dec-24-2017, 06:39 AM
Last Post: DeaD_EyE
  Iterating over pandas.df to check for values out of range Padowan 14 4,626 Nov-26-2017, 04:37 PM
Last Post: heiner55
  check conenction takes byte or not viens 1 798 Nov-01-2017, 06:45 PM
Last Post: nilamo
  "Call the subroutine in order to check..." misa_san 1 1,024 Sep-24-2017, 09:30 AM
Last Post: stranac
  How can I check out my localhost? Mike Ru 2 1,251 Aug-05-2017, 05:50 PM
Last Post: Mike Ru

Forum Jump:


Users browsing this thread: 1 Guest(s)