删除换行符无效

2024-05-23 15:08:32 发布

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

我试图从函数中创建的列表中删除\n。我删除它的代码似乎不起作用。我也没有得到一个错误??你知道吗

代码

#!/usr/bin/python

"""
Description:

Basic Domain bruteforcer

Usage:
  your_script.py (-f <file>) (-d <domain>) [-t 10] [-v]
  your_script.py -h | --help

Arguments:
  -f --file File to read potential Sub-domains from. (Required)
  -d --domain Domain to bruteforce. (Required)
Options:
  -h --help     Show this screen.
  -p --proxy    Proxy address and port. [default: http://127.0.0.1:8080] (Optional)
  -t --thread   Thread count. (Optional)
  -v --verbose  Turn debug on. (Optional)
"""
from docopt import docopt


def fread(dwords, *args):
        flist = open(dwords).readlines()
        #print current list
        print flist
        nlist = flist
        for i in nlist:
          i.rstrip('\n')
          return nlist

if __name__ == "__main__":
        arguments = docopt(__doc__, version='0.1a')
        # print new list with removed \n
        print fread(arguments['--file'])

Tags: to代码frompyyourdomainscripthelp
1条回答
网友
1楼 · 发布于 2024-05-23 15:08:32

字符串是不可变的,i.rstrip('\n')返回一个新字符串。你知道吗

使用列表理解:

def fread(dwords):
    flist = open(dwords).readlines()
    return [s.rstrip('\n') for s in flist]

或者,因为您正在将整个文件读入内存,str.splitlines()

def fread(dwords):
    return open(dwords).read().splitlines()

相关问题 更多 >