[英]How to specify different first words in a line Python
I have a txt file with such lines: 我有一个这样的行的txt文件:
місто Ясинуватського р-ну Донецької обл.#1#Авдіївка
м., ліва притока Інгул.#3#Аджамка (Аджимка, Аджинка
I have done this (Python 2.7): 我做到了这一点(Python 2.7):
for line in text.splitlines():
if line.startswith(u'місто'):
before_keyword, after_keyword = line.rsplit(u'#',1)
encoded=after_keyword.encode('cp1251')
print encoded
How can I specify that my line should starts with u"місто" or u"м."* ? 我怎么可以指定我行应以U“місто”或u 开始 “м。” *? I want my result to be:
我希望我的结果是:
Авдіївка
Аджамка (Аджимка, Аджинка
You can pass a tuple of values to str.startswith
: 您可以将值元组传递给
str.startswith
:
if line.startswith((u'місто', u'м.')):
help on str.startswith
: 有关
str.startswith
帮助:
startswith(...)
S.startswith(prefix[, start[, end]]) -> bool
Return True if
S
starts with the specified prefix, False otherwise.如果
S
以指定的前缀开头,则返回True,否则返回False。 With optional start, test S beginning at that position.通过可选的启动,测试S从该位置开始。 With optional end, stop comparing S at that position.
使用可选结束,停止比较该位置的S. prefix can also be a tuple of strings to try .
前缀也可以是要尝试的字符串元组 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.