美文网首页
read txt file key information

read txt file key information

作者: zxlele_c763 | 来源:发表于2023-08-14 10:39 被阅读0次

读 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)

相关文章

网友评论

      本文标题:read txt file key information

      本文链接:https://www.haomeiwen.com/subject/jetcmdtx.html