简体   繁体   English

如何在Python行中指定不同的第一个单词

[英]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.

相关问题 如何在python中用两个不同的单词拆分两个名字 - How to split two first names that together in two different words in python Python 3.3:如何从每行的第一个单词中提取第一个字母? - Python 3.3: How to extract the first Letters from the first Words on each line? Python-如何计算不同推文中前100个单词的最高tf-idf值 - Python-how to calculate the highest tf-idf value of the first 100 words in different tweeets 使用 Python 在每行的第一个和第二个单词后插入逗号? - Insert commas after the first and second words in each line using Python? 列出Python中文本文件中每行的第一个单词 - List the first words per line from a text file in Python 如何读取和比较以utf-8格式保存的文件的单行中的不同单词? 在python? - how to read and compare different words in a single line of a file which is saved in utf-8 format? in python? 如何在python中指定每一行的字符数? - How to specify number of characters in each line in python? 在python正则表达式中指定匹配换行的不同方法 - Different way to specify matching new line in Python regex 如何读取一行并忽略一些单词(python) - how to read a line and ignore some words (python) 从第一行获取单词直到第一个空格并且在python中没有第一个字符 - Get words from first line until first space and without first character in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM