read txt file key information
2023-08-14 本文已影响0人
zxlele_c763
读 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)