简体   繁体   English

从python中的字符串中删除子字符串

[英]Remove substring from a string in python

I have got a file in python with filenames. 我有一个带有文件名的python文件。 I want to delete some lines and some substirng of the filename using python code. 我想使用python代码删除文件名的某些行和替换。 My file format is the above: 我的文件格式如下:

img/1.jpg
img/10.jpg
img/100.jpg 0 143 84 227
...

I want to delete the img/substring from all the file and the lines where the coordinates are missing. 我想从所有文件和缺少坐标的行中删除img / substring。 For the second task I did the following: 对于第二项任务,我执行了以下操作:

for con in content:
  if ".jpg\n" in con:
        content.remove(con)

for con in content:
    print con

However content didn't change. 但是内容没有改变。

You're attempting to modify the list content while iterating over it. 您试图在迭代列表content时对其进行修改。 This will very quickly bite you in the knees. 这将很快咬住你的膝盖。

Instead, in python you generate a new list: 而是在python中生成一个新列表:

>>> content = [fn for fn in content if not fn.endswith(".jpg\n")]
>>> 

After this you can overwrite the file you read from with the contents from... contents . 之后,您可以使用... contents的内容覆盖您读取的文件。 The above example assumes there is no whitespace to accomodate for in between the filename and the newline. 上面的示例假定在文件名和换行符之间没有空格可容纳。

The error in your current method is because you are iterating through each line by letter , for l in somestring: will go letter by letter. 当前方法中的错误是因为您要逐字母迭代, for l in somestring:会逐字母进行。 Obviously, a ".jpg\\n" won't be in a single letter, so you never hit content.remove(con) . 显然, ".jpg\\n"不会出现在单个字母中,因此您永远不会点击content.remove(con)

I would suggest a slightly different approach: 我建议一种稍微不同的方法:

with open("fileofdata.txt", 'r') as f:
    content = [line for line in f.readlines() if len(line.split()) > 1]

Using len(line.split()) is more robust than line.endswith() because it allows for withspace between .jpg and \\n . 使用len(line.split())line.endswith()更健壮,因为它允许.jpg\\n之间有空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM