Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.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_String - Fatal编程技术网

删除python中从特定字符开始的字符串

删除python中从特定字符开始的字符串,python,string,Python,String,我的文件中包含以下数据: Neighbor V TblVer InQ OutQ Up/Down State/PfxRcd 10.230.2.91 4 136041 0 0 35w6d 1178 CHN_RAY_901_1AC_CASR903R004# exit Neighbor V TblVer InQ OutQ Up/Down State/PfxRcd 10.229

我的文件中包含以下数据:

Neighbor        V          TblVer  InQ OutQ Up/Down  State/PfxRcd
10.230.2.91     4          136041    0    0 35w6d        1178
CHN_RAY_901_1AC_CASR903R004#   exit

Neighbor        V          TblVer  InQ OutQ Up/Down  State/PfxRcd
10.229.5.239    4          890585    0    0 7w5d         1177
10.229.6.239    4          890585    0    0 7w5d         1173
CHN_MAR_905_1AC_CASR903R066#   exit

10.229.30.110
我必须删除从CHN开始的行,并具有如下输出:

Neighbor        V          TblVer  InQ OutQ Up/Down  State/PfxRcd
10.230.2.91     4          136041    0    0 35w6d        1178

Neighbor        V          TblVer  InQ OutQ Up/Down  State/PfxRcd
10.229.5.239    4          890585    0    0 7w5d         1177
10.229.6.239    4          890585    0    0 7w5d         1173

10.229.30.110
我试过:

b = ' '.join(word for word in content.split(' ') if not word.startswith('CHN'))
其中内容是我想从中删除CHN的数据,但它不起作用


你能建议实现这一目标的方法吗。不使用正则表达式可以做到这一点吗?提前感谢。

使用单个行而不是整个文件内容可能更容易:

with open("file") as f:
    lines = [line for line in f if not line.startswith("CHN")]
    filtered = "".join(lines)

使用单个行而不是整个文件内容可能更容易:

with open("file") as f:
    lines = [line for line in f if not line.startswith("CHN")]
    filtered = "".join(lines)
尝试content.splitlines而不是content.split“”。尝试content.splitlines而不是content.split“”。
file = """    Neighbor        V          TblVer  InQ OutQ Up/Down  State/PfxRcd
10.230.2.91     4          136041    0    0 35w6d        1178
CHN_RAY_901_1AC_CASR903R004#   exit

Neighbor        V          TblVer  InQ OutQ Up/Down  State/PfxRcd
10.229.5.239    4          890585    0    0 7w5d         1177
10.229.6.239    4          890585    0    0 7w5d         1173
CHN_MAR_905_1AC_CASR903R066#   exit

10.229.30.110
"""

output = [line for line in file.splitlines() if not line.startswith('CHN')]
print "\n".join(output)