Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python将数据合并到两个文件中_Python_Regex - Fatal编程技术网

Python将数据合并到两个文件中

Python将数据合并到两个文件中,python,regex,Python,Regex,我有两个文本文件。一个文本文件是“numbers.txt”。它包含10位数的电话号码,每行一个。第二个文件“users”包含有关多个帐户的数据。我只想找到numbers.txt中列出的帐户的信息 因此,对于numbers.txt中的每个数字,请在用户文件中搜索该数字。如果找到,则返回该行文本和下一行文本(或返回所有文本,直到下一个空行也可用) numbers.txt看起来像: 1234567021 1234566792 1234567021@host.com User-Password ==

我有两个文本文件。一个文本文件是“numbers.txt”。它包含10位数的电话号码,每行一个。第二个文件“users”包含有关多个帐户的数据。我只想找到numbers.txt中列出的帐户的信息

因此,对于numbers.txt中的每个数字,请在用户文件中搜索该数字。如果找到,则返回该行文本和下一行文本(或返回所有文本,直到下一个空行也可用)

numbers.txt看起来像:

1234567021
1234566792
1234567021@host.com User-Password == "secret"
           Framed-IP-Address = 192.168.1.100,
用户文件看起来像:

1234567021
1234566792
1234567021@host.com User-Password == "secret"
           Framed-IP-Address = 192.168.1.100,
我想要的结果是:

1234567021 1234567021@host.com User-Password == "secret" Framed-IP-Address = 192.168.1.100
我对如何接近它感到困惑。到目前为止,我所拥有的:

#!/usr/bin/env python

import os

# Load numbers text file
if os.path.isfile("numbers.txt"):
    print "Loaded Numbers"
    #### Open file, if exists
    numbers = open('numbers.txt', 'r')
else:
    print "ERROR: Unable to read numbers.txt"
    quit()

# Load user data file
if os.path.isfile("users.txt"):
    print "Loaded user data"
    #### Open file, if exists
    users_data = open('users.txt', 'r')
else:
    print "ERROR: Unable to read users_data"
    quit()


#### Search 
if any(str(users_data) in s for s in numbers):
    for line in numbers:
        if number in line:
            #### Produce sanitized list of output
            output = line.split(' ')
            #print output[0]
            print output
            # also need next line from users_data
            # after each match 

#### Close numbers file and quit
numbers.close()
users_data.close()
quit()

代码不是最优的,因此必须多次读取用户的\u data numbers.txt行:

#### Search
for number in numbers:
    for data in users_data:
        if data.startswith(number):
            print (number, data)

我只是建议您可以先对数据进行排序,然后我们可以循环查找数字中的数字。可以在用户数据中找到数字。

这是用Python 3编写的,以获得我想要的
StringIO
的行为

只需将带有StringIO(nums_txt)的
更改为f:
,将
打开('numbers.txt')更改为f:
,即可使用nums文件的文件名和用户文件部分的名称。这应该是显而易见的:

nums_txt='''\
1234567021
1234566792'''

users='''
1234567021@host.com User-Password == "secret"
           Framed-IP-Address = 192.168.1.100,
''' 

import re
from io import StringIO

with StringIO(nums_txt) as f:   # with open('numbers.txt') as f:  ...
    nums={line.strip():'Not Found' for line in f}

nfs={}    
with StringIO(users) as f:      # with open('users.txt') as f: ...
    for m in re.finditer(r'(^\d{10})(@.*?)(?=(?:\d{10}@)|\Z)', f.read(), re.S | re.M):
        rec=re.sub(r'\s{2,}', ' ', ' '.join(m.group(2).splitlines()))
        if m.group(1) in nums:
            nums[m.group(1)]=rec
        else:
            nfs[m.group(1)]='Not Found'    
print(nums)
印刷品:

{'1234567021': '@host.com User-Password == "secret" Framed-IP-Address = 192.168.1.100,', '1234566792': 'Not Found'}
评论:

  • 如果
    用户
    文件的格式是这样的,则不明显。相应地调整正则表达式
  • 仅当
    数字中的数字是唯一的时才有效
  • users
    中的记录在
    numbers
    中没有相应的编号,这些记录被收集到dict
    nfs

  • 把数字读成一组

    with open('numbers.txt') as f:
        numbers = {line.strip() for line in f if line.strip()}
    
    查看
    users.txt
    中每行的前十个字符。如果该字符串位于
    numbers
    中,则将两行保存到一个容器中(
    dict


    “我卡住了”是什么意思?您看过文档中的吗?-它可能包含一些有用的信息。
    any(s中的“@host”表示数字中的s)
    将始终计算为false-数字中没有
    @host
    字符串。将其更改为
    users\u data
    。我不确定我做错了什么/如何解决问题。在搜索部分,我尝试遍历numbers.txt中的数字,并在users\u数据中搜索所述数字。然后打印关于该数字的数据。顺便说一下,Python代码的标准缩进是。将数字读入一个集合。迭代“users”文件的行,如果一行的前十个字符在集合中,则保存该行和下一行,重复。输出正是我想要的。但是,电话号码与显示的数据不匹配。因此,可能需要按照btrbt的说明首先对数据进行排序。