您好我有一个列表,列表中的每个项目在开头都有相同的字符。如何删除这些字符。 &#34; SITENAME ........................................&#34; < / p>
我试过使用result.split()它会抛出一个错误。这是我到目前为止的代码和
下面的输出createlist = open('ztest12.txt', 'r')
result = [line.split(',') for line in createlist.readlines()]
print result
[['Site Name........................................ Denver\n'],
['Site Name........................................ DenverOutdoor\n'],
['Site Name........................................ Dietzenbach\n'],
['Site Name........................................ EG3\n'],
['Site Name........................................ Edina\n'],
['Site Name........................................ Hongkong\n'],
['Site Name........................................ Honolulu\n'],
['Site Name........................................ HooneePlace\n'],
['Site Name........................................ Juarez\n'],
['Site Name........................................ KansasCity\n'],
['Site Name........................................ Kent\n'],
['Site Name........................................ LA\n'],
['Site Name........................................ Lanham\n'],
['Site Name........................................ Lebanon\n'],
['Site Name........................................ Lockbourne\n'],
['Site Name........................................ London\n'],
['Site Name........................................ Mesa\n'],
['Site Name........................................ Minneapolis\n'],
['Site Name........................................ NewMexico\n'],
['Site Name........................................ NewYork']]
答案 0 :(得分:0)
将它们分开:
with open('ztest12.txt', 'r') as f:
result = [line[50:].rstrip() for line in f]
答案 1 :(得分:0)
我看到两个选项:
清理一个:使用正则表达式
快速而肮脏的一个:
clean = line [50:]#保留第50个
答案 2 :(得分:0)
使用&#34;切片&#34;概念:
>>> x = "Hello World!"
>>> x[2:]
'llo World!'
>>> x[:2]
'He'
希望它有所帮助。
答案 3 :(得分:0)
您尝试在逗号上拆分字符串,但其中包含 no 逗号。无论如何,这是replace
的工作,而不是split
:
createlist = open('ztest12.txt', 'r')
result = [ line.replace('Site Name........................................ ', '') for line in createlist ]
答案 4 :(得分:0)
您可以使用str.rsplit()
来完成此任务:
>>> data = [['Site Name........................................ Denver\n'],
['Site Name........................................ DenverOutdoor\n'],
['Site Name........................................ Dietzenbach\n'],
['Site Name........................................ EG3\n'],
['Site Name........................................ Edina\n'],
['Site Name........................................ Hongkong\n'],
['Site Name........................................ Honolulu\n'],
['Site Name........................................ HooneePlace\n'],
['Site Name........................................ Juarez\n'],
['Site Name........................................ KansasCity\n'],
['Site Name........................................ Kent\n'],
['Site Name........................................ LA\n'],
['Site Name........................................ Lanham\n'],
['Site Name........................................ Lebanon\n'],
['Site Name........................................ Lockbourne\n'],
['Site Name........................................ London\n'],
['Site Name........................................ Mesa\n'],
['Site Name........................................ Minneapolis\n'],
['Site Name........................................ NewMexico\n'],
['Site Name........................................ NewYork']]
>>>
>>> [[s[0].rsplit('.')[-1]] for s in data]
[[' Denver\n'],
[' DenverOutdoor\n'],
[' Dietzenbach\n'],
[' EG3\n'],
[' Edina\n'],
[' Hongkong\n'],
[' Honolulu\n'],
[' HooneePlace\n'],
[' Juarez\n'],
[' KansasCity\n'],
[' Kent\n'],
[' LA\n'],
[' Lanham\n'],
[' Lebanon\n'],
[' Lockbourne\n'],
[' London\n'],
[' Mesa\n'],
[' Minneapolis\n'],
[' NewMexico\n'],
[' NewYork']]
>>>
然而,也许更强大的解决方案是使用re.sub()
:
>>> from re import sub
>>>
>>> [[sub('Site Name\.*', '', s[0])] for s in data]
[[' Denver\n'],
[' DenverOutdoor\n'],
[' Dietzenbach\n'],
[' EG3\n'],
[' Edina\n'],
[' Hongkong\n'],
[' Honolulu\n'],
[' HooneePlace\n'],
[' Juarez\n'],
[' KansasCity\n'],
[' Kent\n'],
[' LA\n'],
[' Lanham\n'],
[' Lebanon\n'],
[' Lockbourne\n'],
[' London\n'],
[' Mesa\n'],
[' Minneapolis\n'],
[' NewMexico\n'],
[' NewYork']]
>>>