1. substring

  1. str.replace() : find and replace
fav_color = "red is my favorite color"
fav_color = fav_color.replace("red", "blue")
print(fav_color)

输出:

blue is my favorite color

how to clean data using Python (string part)how to clean data using Python (string part)
2. str.title():returns a copy of the string with the first letter of each word transformed to uppercase how to clean data using Python (string part)
3. 用函数去掉字符串中不需要的符号

test_data = ["1912", "1929", "1913-1923",
             "(1951)", "1994", "1934",
             "c. 1915", "1995", "c. 1912",
             "(1988)", "2002", "1957-1959",
             "c. 1955.", "c. 1970's", 
             "C. 1990-1999"]

bad_chars = ["(",")","c","C",".","s","'", " "]#需要删除的字符
bad_chars = ["(",")","c","C",".","s","'", " "]
def strip_characters(string):
    for char in bad_chars:
        string = string.replace(char,"")
    return string

stripped_test_data = []
for d in test_data:
    date = strip_characters(d)
    stripped_test_data.append(date)
  1. str.split(): split a CSV from one single string into a list of strings and then into a lists of lists. how to clean data using Python (string part)

  2. how to clean data using Python (string part)

  3. str.format(): inserting values into strings.how to clean data using Python (string part)→→→ how to clean data using Python (string part)并且convert string from integer. 或者用key argumentshow to clean data using Python (string part)how to clean data using Python (string part)

  4. str.startswith()检查开头字符是否为我们要找的
    how to clean data using Python (string part)

  5. how to clean data using Python (string part)

相关文章: