.strip()对于超长字符串无效。
.strip()对于超长字符串无效。
我对\"\\n\"删除问题感到非常沮丧。\n虽然对于短字符串它能完美运作,比如:\n
str1 = "\n 星巴克\n拥有\n最好的咖啡\n" print(type(str1)) newstr = str1.strip() print(newstr)
\n输出:\n
星巴克 拥有 最好的咖啡
\n...但是当我尝试对超长字符串进行相同操作时,它完全无效(这两个字符串的类型相同:
192.168.113.115\nadd server H_ARCAAZ13XV 192.168.113.117\nadd server H_ARCAAZ11LG 192.168.213.154\nadd server H_ARCAAZ12LG 192.168.213.156\nadd server H_ARCAAZ13LG 192.168.213.158\nadd server H_ARCAAZ11RF 192.168.213.153\nadd server H_ARCAAZ12RF 192.168.213.155\nadd server H_ARCAAZ13RF 192.168.213.157\nadd server H_DE1-S912 172.25.235.20\nadd server H_DE1-S911 192.168.238.40\nadd server H_EXCMBX12XV 192.168.114.18\nadd server H_EXCMBX13XV 192.168.114.20\nadd server H_EXCMBX14XV 192.168.114.21\nadd server H_PBIAAZ01XV 192.168.137.182\nadd server H_PBIAAZ02XV 192.168.137.183\nadd server H_PBIAAZ03XV 192.168.137.184\nadd server H_PBIAAZ05DR 192.168.104.104\nadd server H_PBIAAZ06DR 192.168.104.108\nadd server H_PBIAAZ07DR 192.168.104.106\nadd server H_PBIAAZ08DR 192.168.104.110\nadd server H_SSMSM05XV 192.168.120.235\nadd server H_OPEAAZ01XV 192.168.112.51\nadd server H_OPEAAZ02XV 192.168.112.52\nadd server H_CUBAAZ11LG 192.168.213.148\nadd server H_CUBAAZ12LG 192.168.213.166\nadd server H_CUBAAZ11XV 192.168.113.125\nadd server H_CUBAAZ12XV 192.168.113.126\nadd server H_SPSWEB01AZ 192.168.210.34\nadd server H_SPSWEB02AZ 192.168.210.35\nadd server H_EMGAAZ01XV 192.168.113.127\nadd server H_EMGAAZ02XV 192.168.113.128\nadd server H_PBIAAZ09XV 192.168.137.191\nadd server H_IMOSAAZ01AZ 192.168.212.57\nadd server H_IMOSAAZ02AZ 192.168.212.58\nadd server 192.168.206.178 192.168.206.178\nadd server opeaAZ02qa 192.168.206.181\nadd server H_LYCEPS01XV 192.168.125.51\nadd server H_LYCEPS02XV 192.168.125.52\nadd server H_LYCEPS03XV 192.168.125.53\nadd server H_OFFAAZ01XV 192.168.120.152\nadd server H_OFFAAZ02XV 192.168.120.153\nadd server H_CTXSTF01XV 192.168.128.111\nadd server H_CTXSTF02XV 192.168.128.110\nadd server H_SPSLGL01XV 192.168.110.21\nadd server H_SPSLGL02XV 192.168.110.22\nadd server H_FAX01LONUK 192.168.115.11\nadd server H_FAX02LONUK 192.168.115.12\nadd server H_ADSVR01XV 192.168.120.163\nadd server H_ADSVR02XV 192.168.120.164\nadd server H_MSLG-XVSAAZ01 192.168.206.75\nadd server H_MSLG-XVSAAZ02 192.168.206.74\nadd server H_MSXV-XVSAAZ01 192.168.106.152\nadd server H_MSXV-XVSAAZ02 192.168.106.153\nadd server 172.20.0.20 172.20.0.20\nadd server 172.20.0.10 172.20.0.10\nadd server 172.20.0.101 172.20.0.101\nadd server H_EPMWEB01XV 192.168.119.47\nadd server H_EPMWEB02XV 192.168.119.48\nadd server 192.168.206.171 192.168.206.171\nadd server 192.168.206.58 192.168.206.58\nadd server 192.168.213.160 192.168.213.160\nadd server 192.168.213.159 192.168.213.159\nadd server 192.168.213.162 192.168.213.162\nadd server 192.168.213.65 192.168.213.65\nadd server 192.168.213.169 192.168.213.169\nadd server 192.168.112.113 192.168.112.113\nadd server H_WGXV-FSMAAZ02 172.16.49.15\nadd server H_WGCN-FSMAAZ02 172.16.49.16\nadd server H_WGXV-SPTAAZ01 172.16.21.170\nadd server H_WGXV-SPTAAZ02 172.16.21.169\nadd server H_WGCN-SPTAAZ01 172.16.1.155\nadd server H_WGCN-SPTAAZ02 172.16.1.157\nadd server H_MSLG-EWASVR01 192.168.206.77\nadd server H_MSLG-EWASVR02 192.168.206.78\nadd server H_MSXV-MGOOPS01 192.168.120.139\nadd server H_MSXV-MGOOPS03 192.168.120.140\nadd server H_EPMWEB01AZ 192.168.219.30\nadd server H_EPMWEB02AZ 192.168.219.31\nadd server H_CUBWEB01XV 192.168.113.28\nadd server H_CUBWEB02XV 192.168.113.29\nadd server H_EPMAAZ01XV 192.168.119.49\nadd server H_EPMAAZ02XV 192.168.119.50\nadd server H_EPMAAZ01AZ 192.168.219.32\nadd server H_EPMAAZ02AZ 192.168.219.33\nadd server H_STSAAZ01LG 192.168.206.51\nadd server H_STSAAZ02LG 192.168.206.52\nadd server H_RVCAAZ01LG 192.168.213.115\nadd server H_RVCAAZ02LG 192.168.213.116\nadd server H_RVCAAZ03LG 192.168.213.61\nadd server H_RVCAAZ04LG 192.168.213.62\nadd server H_MSLG-MODAAZ01 192.168.206.92 -comment NZ871779\nadd server H_MSLG-MODAAZ02 192.168.206.91 -comment NZ871779\nadd server H_MSXV-MODAAZ01 192.168.106.158\nadd server H_MSXV-MODAAZ02 192.168.106.159\nadd server H_MSIT-XVSAAZ01 192.168.206.162\nadd server H_MSIT-XVSAAZ02 192.168.206.163\nadd server H_MSXV-EWASVR01 192.168.106.154\nadd server H_MSXV-EWASVR02 192.168.106.155\nadd server H_MSIT-MODAAZ01 192.168.222.109\nadd server H_MSIT-MODAAZ02 192.168.222.110\nadd server H_MSLG-XVSWEB01 192.168.222.106\nadd server H_MSLG-XVSWEB02 192.168.222.105\nadd server H_MSLG-MODAAZ03 192.168.222.116\nadd server H_MSLG-MODAAZ04 192.168.222.117\nadd server H_BISIS01XV01 192.168.118.15\nadd server H_BISIS01XV02 192.168.118.16\nadd server H_MSXV-XVSWEB01 192.168.113.134\nadd server H_MSXV-XVSWEB02 192.168.113.135\nadd server H_MSFF-EWASVR01 192.168.222.135\nadd server H_MSFF-EWASVR02 192.168.222.136\nadd server H_MSIT-MODAAZ03 192.168.222.148\nadd server H_MSIT-MODAAZ04 192.168.222.57\nadd server H_MSFF-XVSWEB01 192.168.222.146\nadd server H_MSFF-XVSWEB02 192.168.222.147\nadd server H_MSIT-XVSWEB02 192.168.222.60\nadd server H_MSIT-XVSWEB01 192.168.222.59\nadd server H_MSXV-SPSAAZ05 192.168.110.55\nadd server H_MSXV-SPSAAZ06 192.168.110.57\nadd server H_MSXV-SPSAAZ07 192.168.110.58\nadd server H_MSXV-SPSAAZ08 192.168.110.60\nadd server H_MSXV-SPSAAZ11 192.168.110.65\nadd server H_MSXV-SPSAAZ12 192.168.110.63\nadd server H_MSXV-SPSAAZ13 192.168.110.62\nadd server H_MSXV-SPSAAZ14 192.168.110.64\nadd server H_MSRF-JEASVR01 172.16.1.177\nadd server H_GELG-RTPSFN01 192.168.229.76\nadd server H_GELG-RTPSFN02 192.168.229.77\nadd server H_GELG-RTPSFN03 192.168.229.78\nadd server H_GELG-RTPDSN01 192.168.229.79\nadd server H_GELG-RTPDSN02 192.168.229.81\nadd server H_GELG-RTPDSN03 192.168.229.80\nadd server H_GEXV-RTPSFN01 192.168.113.142\nadd server H_GEXV-RTPSFN02 192.168.113.141\nadd server H_GEXV-RTPSFN03 192.168.113.143\nadd server H_GEXV-RTPDSN01 192.168.113.156\nadd server H_GEXV-RTPDSN02 192.168.113.153\nadd server H_GEXV-RTPDSN03 192.168.113.155\nadd server H_CUBAAZ11LG2 192.168.206.180\nadd server H_MSLG-ABAAAZ01 192.168.221.61\nadd server H_MSLG-ABAAAZ02 192.168.227.217\nadd server H_WGRF-GSXAAZ03 172.16.1.179\nadd server H_WGQA-GSXAAZ01 172.16.1.180\nadd server H_WGCN-GSXAAZ02 172.16.1.181\nadd server H_WGCN-GSXAAZ03 172.16.1.182\nadd server H_WGXV-GSXAAZ02 172.16.21.166\nadd server H_WGXV-GSXAAZ03 172.16.21.182\nadd server 192.168.240.180 192.168.240.180\nadd server 10.3.74.180 10.3.74.180\nadd server H_SSXV-TSGSVR11 192.168.120.148\nadd server H_MSXV-ABAAAZ01 192.168.113.151\nadd server H_MSXV-ABAAAZ02 192.168.113.145\nadd server H_MSANOD11AZ 192.168.206.35\nadd server H_MSANOD12AZ 192.168.206.254\n
.strip()方法不适用于超长字符串的情况。它默认从字符串的开头和末尾删除给定的字符(默认为空格),而不是从字符串中间删除。你之所以认为它删除了\n的出现,是因为print()函数的工作方式:它将\n执行为换行符。请注意,你认为被删除了\n的短字符串实际上被打印成了多行。
相反,你应该使用str.replace()方法,像这样:
newstr = str1.replace('\n', '')
这个方法也适用于较长的字符串,因为字符串的长度从来不是问题。
不幸的是,.replace('\n', '')对我无用,\n仍然存在:
with open('IntDC1.txt') as f:
mylist = f.read()
conf = mylist.replace('\n', '')
print(conf)
如果你从文件中读取文本并将其存储在行的列表中,你可以使用''.join(mylist)将这些行连接成一个字符串。
很遗憾,这也解决不了问题:
with open('IntDC1.txt') as f:
print (" ".join(line.rstrip('\n') for line in f))
我刚刚想到了这个问题的答案!!!文件本身就是一个超长字符串,它是通过get.requests创建的:
lbvs_response=requests.get("https://192.168.150.7/nitro/v1/config/nsrunningconfig",auth=(NITRO_USER, NITRO_PWD),verify=False)
Configuration=lbvs_response.text
WriteImplementationScriptToFile=open('IntDC1.txt', "a")
WriteImplementationScriptToFile.write(str(Configuration))
我从get.request获取的数据已经包含了这些"\n",所以我的任务是相反的 - 我需要删除\n并将这个超长字符串分割成行。
好的,那么你可以使用str.split('\n'),其中str是长字符串的名称。这将返回一个行的列表。