简体   繁体   English

如何通过python3删除以函数开头的行

[英]How to delete a line with starts with function by python3

I want to delete a line in 50,000 wikipages. 我想在50,000个Wiki页面中删除一行。 Because that line is a dead external web page link. 因为该行是无效的外部网页链接。 For example, have a look at this wiki page. 例如,查看此Wiki页面。 The line consists of two parts. 该行包括两部分。 one is starting text another is the URL. 一个是起始文本,另一个是URL。 THe URL varies for each and every word but the text always starts with '*தமிழ் இணையப் பல்கலைக்கழக அகரமுதலியில் '. 每个单词的URL都不同,但文本始终以'*தமிழ்இணையப்அகரமுதலியில்'开头。 How can i delete the line which starts with fuction? 如何删除以功能开头的行?

Sample code :

#!/usr/bin/env python
#-*- coding: utf-8 -*-
wikiPage = '''==உசாத்துணை== 
* தமிழ் இணையப் பல்கலைக்கழக அகரமுதலியில்  [http://www.tamilvu.org/slet/servlet/o33.o33searh?CboSelect=1&TxtSearch=abdominal+muscle&OptSearch=&id=All abdominal muscle]'''

# part1 is a line of  'starts with'
part1 = '* தமிழ் இணையப் பல்கலைக்கழக அகரமுதலியில் '
part2 = '[http://www.tamilvu.org/slet/servlet/o33.o33searh?CboSelect=1&TxtSearch=abdominal+muscle&OptSearch=&id=All abdominal muscle]'
print(wikiPage.replace('part1',''))

How to delete part2 too? 怎样也删除part2? Please note that part2 is a url which differs fro every wiki page. 请注意,第2部分是每个Wiki页面都不同的URL。

Create a new list (or however you store lines) of lines, then iterate through the old list, and only add lines that don't start with that string. 创建一个新的行列表(或存储行),然后遍历旧列表,只添加不以该字符串开头的行。

START_OF_LINE = "*தமிழ் இணையப் பல்கலைக்கழக அகரமுதலியில்"
new_lines = [] # or however you store lines

for line in lines:
    if line.startswith(START_OF_LINE):
        pass
    else:
        new_lines.append(line)
s_line = '*தமிழ் இணையப் பல்கலைக்கழக அகரமுதலியில்'

lines = [line for line in lines if not line.startswith(s_line)]

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

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