簡體   English   中英

Python re.sub 從特定索引開始刪除?

[英]Python re.sub remove starting from specific index?

我有一個腳本,該腳本通過刪除僅名義上的元素來獲取數據並從本質上對其進行清理。 我想知道如何調整remove='^[0-9.]+$'以從特定索引開始,比如說索引 4? 現在它掃描了每個索引。


    def split_lines(fp, delimiter, remove='^[0-9.]+$'):
        with open(fp, mode="r", encoding="utf-8") as file:
            clean_list = []
            for line in file:
                tokens = line.split(delimiter)
                tokens = [re.sub(remove, "", token) for token in tokens]
                clean_list.append(list(filter(lambda e: e.strip(), tokens)))
            txt_edit.delete("1.0", tk.END)
            unique_data = {}
            for item in clean_list:
                key = str(item)
                if not unique_data.get(key):
                    unique_data[key] = 1, item
                else:
                    unique_data[key] = (unique_data[key][0] + 1), item
            for k, v in unique_data.items():
                txt_edit.insert(tk.END, f"{v[1]}x {v[0]} \n")

最簡單的方法可能是只對字符串的一部分運行清理:

my_string = "wowMuchCool"
part_1, part2 = my_string[:4], mystring[4:] # split at the 4th char, so "wowM" and "uchCool"
part_2 = clean_function(part_2) # Let's say it removes "o" here, part_2 = "uchCl"
my_string_cleaned = part_1 + part_2 # "wowMuchCl", the first "o" is untouched

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM