相关疑难解决方法(0)

如何将字符串拆分为列表?

我希望我的Python函数分割一个句子(输入)并将每个单词存储在一个列表中.我当前的代码拆分了句子,但没有将单词存储为列表.我怎么做?

def split_line(text):

    # split the text
    words = text.split()

    # for each word in the line:
    for word in words:

        # print the word
        print(words)
Run Code Online (Sandbox Code Playgroud)

python split list text-segmentation

545
推荐指数
8
解决办法
170万
查看次数

如何通过行而不是变量中的字符进行for循环循环?

我有以下代码ssh到节点并从其他设备中找到RSSI.

bot_ipv6是ssh到的ipv6地址列表,脚本使用pexpect到ssh.

for address in bot_ipv6:
    session=spawn('ssh -6 root@'+address+'%wlan0')
    #session.logfile = stdout
    session.expect('password:')
    session.sendline("123456")
    session.expect(prompt)
    session.sendline("iwlist wlan0 scan")
    session.expect(prompt)
    data=session.before
    session.close()
Run Code Online (Sandbox Code Playgroud)

数据现在包含该iwlist wlan0 scan设备的输出.

我想查看数据并获取地址及其相应的RSSI.当该命令可以在本地运行并输出到文件时,此代码有效:

with open("rssi.txt") as fd:
    for line in fd:
        cell_line = match(line,"Cell ")
        if cell_line != None:
            cells.append([])
            line = cell_line[-27:]
        cells[-1].append(line.rstrip())


for cell in cells:
    level.append(matching_line(cell,"Quality=").split()[2].split('=')[1])
    address.append(matching_line(cell,"Address: "))

scanned=dict(zip(address, level))
Run Code Online (Sandbox Code Playgroud)

匹配和匹配行是其中定义的函数,其中在文件中查找匹配字符的内容.

我的问题是我不知道如何将数据输出到文件,但如果我尝试像使用文件时那样检查输出数据中的内联:

for line in data:
        cell_line = match(line,"Cell ")
        if cell_line != None:
            cells.append([])
            line = cell_line[-27:]
        cells[-1].append(line.rstrip())
Run Code Online (Sandbox Code Playgroud)

而不是线条,它循环每个字符.

如何将输出打印到我可以在本地打开的文件并像之前一样扫描或循环数据中的行而不是单个字符?

这里是一个数据样本的示例(为了得到它我将打印数据放入脚本然后只是从输出中复制并粘贴在这里,希望没有格式化丢失):

iwlist …
Run Code Online (Sandbox Code Playgroud)

python

1
推荐指数
1
解决办法
822
查看次数

标签 统计

python ×2

list ×1

split ×1

text-segmentation ×1