Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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_List_Io - Fatal编程技术网

Python-用数字替换列表中的电子邮件

Python-用数字替换列表中的电子邮件,python,list,io,Python,List,Io,我想用随机数替换文本文件中的所有电子邮件地址。现在我已经找到了这些电子邮件,但它们都是由regex re.findall()返回的列表 例如,输出如下所示: ['xyz123@gmail.com'] 所以当我尝试用随机数替换这个输出时,我会得到一个错误 Can't convert 'list' object to str implicitly 我的代码在这里: with open('a.txt', 'r') as file1: with open('b.txt', 'w') as f

我想用随机数替换文本文件中的所有电子邮件地址。现在我已经找到了这些电子邮件,但它们都是由regex re.findall()返回的列表

例如,输出如下所示:

['xyz123@gmail.com']
所以当我尝试用随机数替换这个输出时,我会得到一个错误

Can't convert 'list' object to str implicitly
我的代码在这里:

with open('a.txt', 'r') as file1:
    with open('b.txt', 'w') as file2:
        for line in file1:
            email = re.findall(......,line)
            file2.write(line.replace(email, random.random()))

其余代码被省略,因为它们在这里没有用处。有谁能告诉我如何处理这份名单吗?我曾尝试显式强制列表使用str()字符串,但失败了

让re模块为您完成所有工作,从而避免转换列表对象的问题。sub或regex pattern object.sub将用以regex match对象作为输入的函数的输出替换与模式匹配的子字符串

#pass each line through this:
def mask_matches( string, regex ):
    ''' substrings of string that match regular expressions pattern object regex
    are replaced with random.random( ).
    '''
    def repl( mobj ):
        replacement = random.random( )
        return( replacement )
    return( regex.sub( repl, string ) )

#so if `email` is your regex pattern object, thenyour last line looks something like this:
file2.write( mask_matches( line, email ) )

通过让re模块为您完成所有工作,避免转换列表对象的问题。sub或regex pattern object.sub将用以regex match对象作为输入的函数的输出替换与模式匹配的子字符串

#pass each line through this:
def mask_matches( string, regex ):
    ''' substrings of string that match regular expressions pattern object regex
    are replaced with random.random( ).
    '''
    def repl( mobj ):
        replacement = random.random( )
        return( replacement )
    return( regex.sub( repl, string ) )

#so if `email` is your regex pattern object, thenyour last line looks something like this:
file2.write( mask_matches( line, email ) )

尝试通过电子邮件循环并替换,如下所示:-

with open('a.txt', 'r') as file1:
    with open('b.txt', 'w') as file2:
        for line in file1:
            email = re.findall(......,line)
            for em in email:
                file2.write(line.replace(em, random.random()))

尝试通过电子邮件循环并替换,如下所示:-

with open('a.txt', 'r') as file1:
    with open('b.txt', 'w') as file2:
        for line in file1:
            email = re.findall(......,line)
            for em in email:
                file2.write(line.replace(em, random.random()))

谢谢你。但我已经试过了,它会返回“不能隐式地将‘float’对象转换为str”我并没有执行这个,是的,问题可能在这里。我们需要换最后一行。像这样使用它并获得成功的执行行。替换下一行文件中的(em,random.random())2.write(STR(line))谢谢。但我已经试过了,它会返回“不能隐式地将‘float’对象转换为str”我并没有执行这个,是的,问题可能在这里。我们需要换最后一行。像这样使用它并获得成功的执行行。替换下一行文件中的(em,random.random())2.write(STR(line))谢谢。但是我会在“return(regex.sub(repl,string))行中收到一个“AttributeError:'list'对象没有属性'sub'”,正如我在代码注释中试图解释的那样,
电子邮件
在我建议的代码中并不是指你的
电子邮件
列表()。抱歉,我做的不够多,您无法复制和粘贴。
电子邮件应为正则表达式模式对象。您不必首先生成列表。您应该有一个以
电子邮件=re.findall
开头的行,而不是以
电子邮件=re.compile
开头的行。您不需要您的正则表达式模式如何,因此我无法编写复制粘贴代码来替换该行。谢谢。但正如我在代码注释中试图解释的那样,我将在“return(regex.sub(repl,string))行中收到一个“AttributeError:'list'object has no attribute'sub'”,我建议的代码中的
电子邮件
并不是指你的
电子邮件
列表()。很抱歉,我没有走得足够远,您无法直接复制和粘贴<代码>电子邮件
应为正则表达式模式对象。您无需首先生成列表。你应该有一行开头是
email=re.findall
,而不是以
email=re.compile
开头。您没有显示正则表达式模式,因此我无法编写复制粘贴代码来替换该行。