python string
1. 去掉字符串末尾的数字。
#去掉字符串末尾的 >> import string >> string.digits '0123456789' >>> t = 'book12345' >>> t.rstrip(string.digits) 'book'
2. 删除字符串末尾的字符,参考:http://www.runoob.com/python/att-string-rstrip.html
# Python rstrip() 删除string 字符串末尾的指定字符(默认为空格)。 #rstrip()方法语法 str.rstrip([chars]) # 参数:chars: --指定 删除的字符 # 返回值: 返回删除string字符串末尾的指定字符后生成的新的字符串。
#Example 如下 >>> a = 'Hello ' >>> a.rstrip() 'Hello' #删除指定字符 >>> a = 'Hello 888' >>> a.rstrip('8') 'Hello '
3.删除括号以及括号中的内容
#正则表达式: >>> import re >>> s = 'Hello (World)! I {Love} you!' >>> a = re.sub(u"\\(.*?\\)|\\{.*?}|\\[.*?]", "", s) >>> a 'Hello ! I you!'
import pandas as pd import string import re energy = pd.read_excel('Energy Indicators.xls', usecols=[2,3,4,5], skiprows=16, skipfooter=38, na_values=['...']) energy1 = energy.drop([0]) for col in energy1.columns: if col[:7] == 'Unnamed': energy1.rename(columns={col:'Country'}, inplace=True) if col[-6:] == 'capita': energy1.rename(columns={col:col[:-6] + 'Capita'}, inplace=True) if col[-10:] == 'Production': energy1.rename(columns={col:'% ' + col[:9]}, inplace=True) #nergy1.reset_index()gy1.set_index('Country') energy1 = energy1.set_index('Country') for row in energy1.index: if row[:17] == "Republic of Korea": energy1.rename(index = {row : 'South Korea'}, inplace=True) if row[:13] == "United States": energy1.rename(index = {row : 'United States'}, inplace=True) if row[:14] == "United Kingdom": energy1.rename(index = {row : 'United Kingdom'}, inplace=True) if row[:16] == "China, Hong Kong": energy1.rename(index = {row : "Hong Kong"}, inplace=True) #删除字符串括号以及括号的内容。 energy1.rename(index = {row : re.sub(u"\\(.*?\\)","",row)}, inplace=True) #删除字符串末尾的数字 energy1.rename(index = {row : row.rstrip(string.digits)}, inplace=True) #删除字符串末尾的空格 energy1.rename(index = {row : row.rstrip()}, inplace=True) energy1
The Safest Way to Get what you Want is to Try and Deserve What you Want.