i have a list:
我有一个清单:
row=['hi', 'there', 'how', ...........'some stuff is here are ','you']
as you can see row[8]='some stuff is here are '
你可以看到row [8] ='有些东西在这里'
if the last character is a space i would like to get everything except for the last character like this:
如果最后一个字符是一个空格我想得到除了最后一个字符之外的所有字符:
if row[8][len(row[8])-1]==' ':
row[8]=row[8][0:len(row[8])-2]
this method is not working. can someone suggest a better syntax please?
这种方法不起作用。有人可以提出更好的语法吗?
3 个解决方案
#1
7
row = [x.strip() for x in row]
(if you just want to get spaces at the end, use rstrip
)
(如果你只是想在最后获得空格,请使用rstrip)
#2
4
Negative indexes count from the end. And slices are anchored before the index given.
负指数从结束开始计算。并且在给出索引之前锚定切片。
if row[8][-1]==' ':
row[8]=row[8][:-1]
#3
4
So you want it without trailing spaces? Can you just use row[8].rstrip
?
所以你想要它没有尾随空格?你能用row [8] .rstrip吗?
#1
7
row = [x.strip() for x in row]
(if you just want to get spaces at the end, use rstrip
)
(如果你只是想在最后获得空格,请使用rstrip)
#2
4
Negative indexes count from the end. And slices are anchored before the index given.
负指数从结束开始计算。并且在给出索引之前锚定切片。
if row[8][-1]==' ':
row[8]=row[8][:-1]
#3
4
So you want it without trailing spaces? Can you just use row[8].rstrip
?
所以你想要它没有尾随空格?你能用row [8] .rstrip吗?