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
#1
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]))
Reply
#2
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".
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
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]))
Reply
#6
Why did you change line[:4] to line[:2] on line 14? I changed that back and got it to work on my machine.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
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.
Reply
#8
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:).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
I dont understand which part that I have to change. Could you please mark in the code?
Reply
#10
Lines 2, 9, and 14.
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,244 Jan-13-2023, 01:35 PM
Last Post: perfringo
  how to check if file path finish on .csv danlopek14q 4 10,952 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