本文介绍了如何使用Python检查文件中是否存在数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这样的txt文件:A1B2C3D4.8Z
我有一个数组数据:[A1,xx,ss,11]
我要检查我的txt文件中的每个2个字符是否存在该文件中的数组数据。
我尝试了什么:
我已经尝试将我的文本数据转换为2D并剪切最后3个字符。现在,我无法弄清楚如何检查数组数据是否存在。这是我的代码。
< pre lang =Python>
outfile =Result.txt
数组= [6J,xx,ss,11]
打开( test.txt,r)为f:
,结果为open(outfile,w):
output_list = []
表示f.read()中的rec。 ():
rec = rec [: - 3]
list = [rec [i:i + 2] for i in range(0,len(rec),2)]
output_list .append(list)
列表中的行:
found = bool(Array.search(line))
result.write(str((0,1)[found ]))
解决方案
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 character from my txt file whether my array data exist in that file or not.
What I have tried:
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.
<pre lang="Python"> 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]))
解决方案
这篇关于如何使用Python检查文件中是否存在数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!