read txt file key information

读 txt 文件 key information,写到 EXCEL中

  • read txt files about the key information

  • write the key information list to excel file.

  • input txt data: key information ueidcu

    图片.png

  • output to excel


    图片.png

python codes


import re
import pandas as pd


def extract_numbers_after_keyword(filename, keyword):
    numbers = []
    with open(filename, 'r') as file:
        contents = file.read()
        # Construct the regex pattern to match the keyword and the number
        pattern = rf'{keyword}\D+(\d+)'
        #pattern = r'ueIdCu\D+(\d+)'
        matches = re.findall(pattern, contents)
        # Extract the numbers from the matches
        numbers = [int(match) for match in matches]
    return numbers

def output2Excel(numbers):
   #df[''] = numbers 
   numbersList = list(numbers)
   data ={'ueidCu':numbersList}
   df = pd.DataFrame(data)
   #df = pd.DataFrame(numbers)
   print("ex#el data frame..")
   print(df)
   # Create a new Excel file
   df.to_excel('example.xlsx', index=False)




# Replace with the actual file path and keyword
filename = 'ueids.txt'
keyword = 'ueIdCu'
numbers = extract_numbers_after_keyword(filename, keyword)

print("ueid numbers is: ", len(numbers))
print(numbers)

numbersSet = set(numbers)
print("ueid set is: ", len(numbersSet))
print(numbersSet)

output2Excel(numbersSet)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容