Python Forum

Full Version: How to check an array exist in a file using Python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I have a txt file like this : A1B2C3D4.8Z
I have an array data : ["A1", "xx", "ss", "11"]

I am going to check each 2 characters from my txt file whether my array data exist in that file or not.

I already try to convert my text data to 2D and cut the 3 last characters. Now, I can not figure out how to check the array data exist or not. Here my code.


outfile = "Result.txt"
Array = ["6J", "xx", "ss", "11"]

with open("test.txt", "r") as f:
	with open(outfile, "w") as result:
		output_list = []
		for rec in f.read().splitlines():
			rec = rec[:-3]  
			list = [rec[i:i+2] for i in range(0, len(rec), 2)] 
			output_list.append(list)

		for line in list:
			found = bool(Array.search(line))
			result.write(str((0,1)[found]))
It depends on how your data is set up. Is the Array you are looking for always going to be at the start of the line? If so, you could use Array == line[:4]. If not, I think you would be better off not converting to a string. That is, search each line for "6Jxxss11".
Ya. the Array is going to be the start of the line. I tried this

outfile = "Result.txt"
Array = ["6J", "xx", "ss", "11"]

with open("test.txt", "r") as f:
	with open(outfile, "w") as result:
		output_list = []
		for rec in f.read().splitlines():
			rec = rec[:-3]  
			list = [rec[i:i+2] for i in range(0, len(rec), 2)] 
			output_list.append(list)

		for line in list:
			found = bool(Array==line[:4])
			result.write(str((0,1)[found]))
BUT IT RETURN 000000000000000000000000
A few things: One, list is a bad name for a list. It's the Python function for creating a list, and by using the name elsewhere you are cutting off code that might mess up something else. Two, Array == line[:4] is already a bool, you don't need to convert it to one. Three, bool is a subclass of int, to convert it to 1 or 0 you just need int(found).

Typing all that up made me figure out the problem: you want the second for loop to be over output_list, not list.
I tried this, but It give me always 0
outfile = "Result.txt"
Array = ["6J", "xx", "ss", "11"]

with open("test.txt", "r") as f:
	with open(outfile, "w") as result:
		output_list = []
		for rec in f.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)

		for line in output_list:
			found = int(Array == line[:2])
			result.write(str((0,1)[found]))
Why did you change line[:4] to line[:2] on line 14? I changed that back and got it to work on my machine.
I already change it to line[:4] , but the result is always 0, even one of the array exist on the output_list. My expectation is when one of my array in the output_list is exist, it should be returned 1.
Then you shouldn't be using lists at all. Array and FBList should be sets. You should check the intersection of the two sets (if ArraySet & FBSet:).
I dont understand which part that I have to change. Could you please mark in the code?
Lines 2, 9, and 14.
Pages: 1 2