开发者

string split in python

I want to split string between a tab. Let's say I have some text in a file.txt

Kern_County_Museum  1,000,000+
Fairplex_railway_exhibit    Depot and rolling stock

So I want to remove redundancy from left side and keep right side as it is.

import re
import string
import urllib

for line开发者_如何学Python in open('file.txt', 'r').readlines():
left, right = string.split(line, maxsplit=1)
relation = string.split(line, maxsplit=1)

le = relation[0]
ri = relation[1]

le = urllib.unquote(relation[0])
le = le.replace('_', ' ')


print le, '\t', ri


Restrain your split.

left, right = line.split(None, 1)


By default split method splits string by any whitespace. To split string by a tab, pass extra parameter to this method:

left, right = line.split('\t', 1)


Use str.partition

left, delim, right = line.partition('\t')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜