读取一个文件中的行,然后查找另一个txt fi中列出的所有以4个字母开头的字符串

2024-09-25 00:35:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我有2个txt文件(a和b)。在

文件_a.txt包含一个4个字母组合的长列表(每行一个组合):

aaaa
bcsg
aacd
gdee
aadw
hwer
etc.

文件_b.txt包含不同长度的字母组合列表(有些带有空格):

^{pr2}$

我正在寻找一个允许我执行以下操作的python脚本:

  1. 逐行读取文件_a.txt
  2. 取每个4个字母的组合(例如aaai)
  3. 阅读文件_b.txt,找到以4个字母组合开头的所有不同长度的字母组合(例如aaaibjkes,aaailoiersaaageehikjaaa,aaailoiuwegoiglkjaaaike等)
  4. 将每次搜索的结果打印在一个以4个字母组合命名的单独txt文件中。在

文件aaai.txt文件公司名称:

aaaibjkes 
aaailoiersaaageehikjaaa
aaailoiuwegoiglkjaaake
etc.

文件密件索引.txt公司名称:

bcspwiopiejowih
bcsiweyoieotpwe
etc.

对不起,我是个新手。有人能给我指一下正确的方向吗。到目前为止,我只有:

#I presume I will have to use regex at some point
import re

file1 = open('file_a.txt', 'r').readlines()
file2 = open('file_b.txt', 'r').readlines()

#Should I look into findall()?

Tags: 文件txt名称列表etc公司openfile
3条回答

我希望这对你有帮助

file1 = open('file_a.txt', 'r')
file2 = open('file_b.txt', 'r')

#get every item in your second file into a list 
mylist = file2.readlines()

# read each line in the first file
while file1.readline():
    searchStr = file1.readline()
    # find this line in your second file
    exists = [s for s in mylist if searchStr in s]
    if (exists):
        # if this line exists in your second file then create a file for it
        fileNew = open(searchStr,'w')
        for line in exists:
            fileNew.write(line)

        fileNew.close()

    file1.close()

试试这个:

f1 = open("a.txt","r").readlines()
f2 = open("b.txt","r").readlines()
file1 = [word.replace("\n","") for word in f1]
file2 = [word.replace("\n","") for word in f2]

data = []
data_dict ={}
for short_word in file1:
    data += ([[short_word,w] for w in file2 if w.startswith(short_word)])

for single_data in data:
    if single_data[0] in data_dict:
        data_dict[single_data[0]].append(single_data[1])
    else:
        data_dict[single_data[0]]=[single_data[1]]

for key,val in data_dict.iteritems():
    open(key+".txt","w").writelines("\n".join(val))
    print(key + ".txt created")

您可以做的是打开两个文件并使用for循环逐行运行这两个文件。在

您可以有两个for循环,第一个循环读取file_a.txt,因为您将只读取一次。第二个将通读file_b.txt,并在开始处查找字符串。在

为此,您必须使用.find()来搜索字符串。因为它在开始处,所以值应该是0。在

file_a = open("file_a.txt", "r")
file_b = open("file_b.txt", "r")

for a_line in file_a:
    # This result value will be written into your new file
    result = ""
    # This is what we will search with
    search_val = a_line.strip("\n")
    print "   Using " + search_val + " from file_a to search.   "
    for b_line in file_b:
        print "Searching file_b using " + b_line.strip("\n")
        if b_line.strip("\n").find(search_val) == 0:
            result += (b_line)
    print "   Search ended   "
    # Set the read pointer to the start of the file again
    file_b.seek(0, 0)

    if result:
        # Write the contents of "results" into a file with the name of "search_val"
        with open(search_val + ".txt", "a") as f:
            f.write(result)

file_a.close()
file_b.close()

测试用例:

我在您的问题中使用测试用例:

文件_a.txt

^{pr2}$

文件

aaaibjkes
aaleoslk
abaaaalkjel
bcsgiweyoieotpwe
csseiolskj
gaelsi asdas
aaaloiersaaageehikjaaa
hwesdaaadf wiibhuehu
bcspwiopiejowih
gdeaes
aaailoiuwegoiglkjaaake

该程序生成一个输出文件bcsg.txt,正如它所期望的那样,其中包含bcsgiweyoieotpwe。在

相关问题 更多 >