簡體   English   中英

Python:從編號列表中刪除數字並刪除帶有某些字符的項目

[英]Python: Removing the numbers from a numbered list and removing items with certain characters

我有一個文本文件,其中包含葯物和化學結構的編號列表。

有什么辦法可以去掉物質名稱前面的數字嗎?

這是我到目前為止的代碼:

new_file = open("string_cleaned.txt", "w")
      
for line in open("string.txt", "r"):
  x = txt.lsplit(", ", 1)[1]
  new_file.write(x)


new_file.close()

目標

From:
1 Substance 1
2 Substance 2

To:
Substance 1
Substance 2

不是防彈解決方案,但如果您的數據像您的示例,它可能會起作用。 如果它需要更多的調整告訴我。

import string

alphabet = string.ascii_lowercase + string.ascii_uppercase

YourFile = open("yourFile.txt", "r")
listOfLines = YourFile.readlines()


for lineIndex in range(len(listOfLines)):
    for char in listOfLines[lineIndex]:
        if char in alphabet:
            editedLine = listOfLines[lineIndex].split(char,1)[1]
            editedLine = str(lineIndex + 1) + "  " + char + editedLine  #(optional) If you need the Index numbers beside your items
            listOfLines[lineIndex] = editedLine
            break

anotherFile = open("anotherFile.txt", "w")
anotherFile.writelines(listOfLines)
anotherFile.close

所以在這里編輯之后是解決方案

YourFile = open("yourFile.txt", "r")
listOfLines = YourFile.readlines()

for index in range(len(listOfLines)):
    listOfLines[index] = listOfLines[index].lstrip("0123456789")
    listOfLines[index] = listOfLines[index].lstrip(" ")

    print(listOfLines[index])


anotherFile = open("anotherFile.txt", "w")
anotherFile.writelines(listOfLines)
anotherFile.close

編輯:一個特定的解決方案。

import re

result = ""
for line in open("string.txt"):
    result += re.sub(r"(?<=\s)[^a-zA-Z]*", "", line)

with open("string_cleaned.txt", "w") as file:
    file.write(result)

暫無
暫無

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

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