简体   繁体   中英

How to specify different first words in a line Python

I have a txt file with such lines:

місто Ясинуватського р-ну Донецької обл.#1#Авдіївка
м., ліва притока Інгул.#3#Аджамка (Аджимка, Аджинка

I have done this (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"м."* ? I want my result to be:

Авдіївка

Аджамка (Аджимка, Аджинка

You can pass a tuple of values to str.startswith :

if line.startswith((u'місто', u'м.')):

help on str.startswith :

startswith(...)
    S.startswith(prefix[, start[, end]]) -> bool

Return True if S starts with the specified prefix, False otherwise. With optional start, test S beginning at that position. With optional end, stop comparing S at that position. prefix can also be a tuple of strings to try .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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