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


Messages In This Thread
How to check an array exist in a file using Python - by fitrisibarani - Feb-21-2019, 09:30 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Newbie here. Create an array from file data? Rayj00 2 1,323 Jan-13-2023, 01:35 PM
Last Post: perfringo
  how to check if file path finish on .csv danlopek14q 4 11,151 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