当前位置: 代码迷 >> 综合 >> how to clean data using Python (string part)
  详细解决方案

how to clean data using Python (string part)

热度:59   发布时间:2023-11-04 21:33:13.0

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

在这里插入图片描述所有的都会被替换
2. str.title():returns a copy of the string with the first letter of each word transformed to uppercase 在这里插入图片描述
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 stringstripped_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. 在这里插入图片描述

  2. 在这里插入图片描述

  3. str.format(): inserting values into strings.普通输出→→→ 之后并且convert string from integer. 或者用key arguments在这里插入图片描述much better

  4. str.startswith()检查开头字符是否为我们要找的
    在这里插入图片描述

  5. 在这里插入图片描述

  相关解决方案