第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 python 操作和替換第一個字符串并維護第二個字符串行

使用 python 操作和替換第一個字符串并維護第二個字符串行

動漫人物 2021-12-09 14:49:19
我有一個由多行數(shù)據(jù)(systemid 和主機名)組成的文件 (sysid.txt),如下所示0192.4500.0000 uue01.re1                              0192.4500.0010 ccu01.re1                              0192.4500.0110 uue02.re1                               0192.4500.0001 core1.re2                                   根據(jù)此處的信息和幫助,第一個字符串(數(shù)字)成功替換為需要但第二個字符串(主機名)丟失,并且當我運行下面的代碼時,輸出顯示在單行中。file1 = open('sysid.txt', 'r')file2 = open('sysip.txt', 'w')file1_cont = file1.readlines()for line in file1_cont:    line = line.replace('.', '')    f = itemgetter(slice(0,3), slice(3,6), slice(6,9), slice(9,12))    line = '.'.join(f(line.replace('.','')))    line = '{}.{}.{}.{}'.format(*map(int, f(line.replace('.', ''))))    file2.write(line)    print(line)sysip.txt 的輸出10.89.0.010.89.0.110.89.0.3210.89.0.3310.89.0.3410.89.0.3510.89.0.64閱讀每一行,我想替換第一個字符串(數(shù)字)并保持第二個字符串(主機名)如下192.45.0.0 uue01.re1                              192.45.0.10 ccu01.re1                              192.45.0.110 uue02.re1                               192.45.0.1 core1.re2我如何操作第一個字符串/數(shù)字并將輸出行(file2.write(line))保存在新行中,同時保持上面的第二個字符串。感謝您的支持和指導。#更新列表.txt...System ID      Hostname                                        0192.4500.0000 uue01.re1                              0192.4500.0010 ccu01.re1                              0192.4500.0110 uue02.re1                               0192.4500.0001 core1.re2 {master}
查看完整描述

2 回答

?
吃雞游戲

TA貢獻1829條經(jīng)驗 獲得超7個贊

列表.txt:


0192.4500.0000 uue01.re1                              

0192.4500.0010 ccu01.re1                              

0192.4500.0110 uue02.re1                               

0192.4500.0001 core1.re2   

因此:


def removeZeros(ip):

    # splits the ip by "."

    # converts the words to integeres to remove leading removeZeros

    # convert back the integer to string and join them back to a string

    new_ip = ".".join([str(int(i)) for i in ip.split(".")])

    return new_ip


logFile = "list.txt"

with open(logFile) as f:

    content = f.readlines()

# you may also want to remove empty lines

content = [l.strip() for l in content if l.strip()]


for line in content:

    line = line[1:].split(" ")[0]

    line =  removeZeros(line).replace(removeZeros(line).split(".", 2)[1],removeZeros(line).split(".", 2)[1][:-2] + ".0")

    print(line)

輸出:


192.45.0.0

192.45.0.10 

192.45.0.110 

192.45.0.1 

編輯:


如果要將新 ip 覆蓋list到同一個文件,可以創(chuàng)建兩個單獨的列表來存儲ips和text:


logFile = "list.txt"

with open(logFile) as f:

    content = f.readlines()

# you may also want to remove empty lines

content = [l.strip() for l in content if l.strip()]

ipList = []

stList = []

for line in content:

    stList.append(line[1:].split(" ")[1])

    line = line[1:].split(" ")[0]

    line =  removeZeros(line).replace(removeZeros(line).split(".", 2)[1],removeZeros(line).split(".", 2)[1][:-2] + ".0")

    ipList.append(line)

    # print(line)


with open(logFile, "w") as f:

    for index in range(len(ipList)):

        f.write(str(ipList[index]) + " " + str(stList[index]) + "\n")

輸出(來自文件):


192.45.0.0 uue01.re1

192.45.0.10 ccu01.re1

192.45.0.110 uue02.re1

192.45.0.1 core1.re2

編輯 3:


為了刪除第一行和最后一行,請使用切片:


替換這一行:


for line in content:

有了這個:


for line in content[1:-1]:  # after the first and before the last line


查看完整回答
反對 回復 2021-12-09
?
撒科打諢

TA貢獻1934條經(jīng)驗 獲得超2個贊

我如何操作第一個字符串/數(shù)字 [...] 并同時保持上面的第二個字符串。


在替換點之前,您似乎可以在空格字符上拆分字符串,只修改您需要的部分。


例子:


s = "0192.4500.0010 ccu01.re1                              "

numbers, host = s.split()

numbers = numbers.replace(".", "")

# TODO: fill in appropriate conversions with `numbers` here

print(numbers, host)

輸出是:


019245000010 ccu01.re1

以供參考:


https://docs.python.org/3/library/stdtypes.html#str.split

我如何 [...] 在新行中保存輸出行 (file2.write(line))


將輸出寫入文件時,不會自動添加換行符。您需要自己將它們添加到字符串中。


例子:


numbers = "{}.{}.{}.{}".format(*numbers)

line = "{} {}\n".format(numbers, host)

outf.write(line)

此外,使用with語句打開和最終關(guān)閉文件也是一個好習慣。


查看完整回答
反對 回復 2021-12-09
  • 2 回答
  • 0 關(guān)注
  • 225 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號