Python Forum
How to check an array exist in a file using Python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: How to check an array exist in a file using Python (/thread-16282.html)

Pages: 1 2


How to check an array exist in a file using Python - fitrisibarani - Feb-21-2019

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]))



RE: How to check an array exist in a file using Python - ichabod801 - Feb-21-2019

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".


RE: How to check an array exist in a file using Python - fitrisibarani - Feb-22-2019

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


RE: How to check an array exist in a file using Python - ichabod801 - Feb-22-2019

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.


RE: How to check an array exist in a file using Python - fitrisibarani - Feb-22-2019

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]))



RE: How to check an array exist in a file using Python - ichabod801 - Feb-22-2019

Why did you change line[:4] to line[:2] on line 14? I changed that back and got it to work on my machine.


RE: How to check an array exist in a file using Python - fitrisibarani - Feb-25-2019

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.


RE: How to check an array exist in a file using Python - ichabod801 - Feb-25-2019

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:).


RE: How to check an array exist in a file using Python - fitrisibarani - Feb-25-2019

I dont understand which part that I have to change. Could you please mark in the code?


RE: How to check an array exist in a file using Python - ichabod801 - Feb-25-2019

Lines 2, 9, and 14.