Python
Python
To extract specific strings from multiple *.txt files in a directory, you can use a scripting
language such as Python. Here is an example script that extracts the name and age from all files
in a directory that contain the known string "person":
import os
known_string = "person"
# Check if the file is a *.txt file and contains the known string
contents = f.read()
# Loop over the strings to extract and check if they are in the line
if string_to_extract in line:
To extract specific strings from multiple *.txt files in a directory, you can use a scripting language such as
Python. Here is an example script that extracts the name and age from all files in a directory that contain
the known string "person":
python
import os
known_string = "person"
# Check if the file is a *.txt file and contains the known string
contents = f.read()
# Loop over the strings to extract and check if they are in the line
if string_to_extract in line:
value = line.split(string_to_extract)[-1].strip()
This script assumes that the name and age are on separate lines and are preceded by the strings "name"
and "age", respectively. If this is not the case, you will need to modify the script accordingly. Also note
that this script only works with *.txt files in the current directory. If the files are in a different directory,
you will need to modify the script to use the correct path.
User
[Model Name],TopZoneVladestiC224e
[Send Date],28/04/23
[Total Counter],00191545
ChatGPT
Based on the example data you provided, here is an updated Python script that extracts the name, serial
number, send date, and total counters:
import os
known_string = "TopZoneVladestiC224e"
strings_to_extract = ["[Model Name]", "[Serial Number]", "[Send Date]", "[Total Counter]", "[Total Color
Counter]", "[Total Black Counter]", "[Total Scan/Fax Counter]"]
# Check if the file is a *.txt file and contains the known string
contents = f.read()
# Loop over the strings to extract and check if they are in the line
if string_to_extract in line:
value = line.split(string_to_extract)[-1].strip()
Note that if there are multiple files that match the known string, the output will contain
information for all of them. Also note that this script assumes that the information is always in
the same order and format. If the format changes, you will need to modify the script accordingly.
what is in square brakets=name; after comma =value. extract all values and import in a excel
files ordered by [Model Name]
ChatGPT
To extract the values from the text file and import them into an Excel file ordered by [Model
Name], you can modify the Python script as follows:
import os
import csv
import openpyxl
known_string = "TopZoneVladestiC224e"
strings_to_extract = {
"[Model Name]": "Model Name",
data = []
# Check if the file is a *.txt file and contains the known string
values = {}
contents = f.read()
# Loop over the strings to extract and check if they are in the line
value = line.split(string_to_extract)[-1].strip()
values[column_name] = value
data.append(values)
workbook = openpyxl.Workbook()
worksheet = workbook.active
worksheet.append(row)
workbook.save("output.xlsx")
This script uses the csv module to write the data to a temporary CSV file, and then uses the openpyxl
module to read the CSV file and write the data to an Excel file. The data is sorted by [Model Name]
before being written to the Excel file.
Note that this script assumes that all of the text files have the same format and that the values are
always in the same order. If the format changes, you will need to modify the script accordingly.
User
yes , but [Model Name] can take multiple values. TopZoneVladestiC224e is JUST one of the value. in next
file can be [secretariat], next [contab], so on
[Model Name],Sanitar.Internat454e
[Send Date],26/04/23
[Total Counter],00153131
If the [Model Name] field can take multiple values, you can modify the script to extract all possible
values and add them to the output Excel file as separate rows. Here's how you can modify the script:
import os
import csv
import openpyxl
strings_to_extract = {
data = []
if filename.endswith(".txt"):
values = {}
contents = f.read()
# Loop over the strings to extract and check if they are in the line
if string_to_extract in line:
value = line.split(string_to_extract)[-1].strip()
values[column_name] = value
# Add the dictionary to the list
new_values = values.copy()
data.append(new_values)
workbook = openpyxl.Workbook()
worksheet = workbook.active
worksheet.append(row)
workbook.save("output.xlsx")
This script extracts all possible [Model Name] values from the text files, and adds a separate row
to the output Excel file for each value. The other fields are duplicated for each row. The data is
sorted by [Model Name] before being written to the Excel file.
[Model Name],Sanitar.Internat454e
[Total Counter],00153131
Model Name Serial Number Send Date Total Counter Total Color Counter Total Black Counter
Total Scan/Fax Counter
Since this file does not have values for [Total Color Counter] and [Total Black Counter],
those columns are filled with "N/A".
User
2/2
[Model Name],TopZoneVladestiC224e
[Send Date],28/04/23
[Total Counter],00191545
User
[Model Name],Sanitar.Internat454e
[Total Counter],00153131
next file
[Send Date],20/04/23
[Total Counter],00463090
Using the modified script I provided earlier, the output for the files you provided would look like
this:
Model Name Serial Number Send Date Total Counter Total Color Counter Total Black Counter
Total Scan/Fax Counter
import os
import csv
import openpyxl
strings_to_extract = {
data = []
if filename.endswith(".txt"):
values = {}
contents = f.read()
# Loop over the strings to extract and check if they are in the line
if string_to_extract in line:
value = line.split(string_to_extract)[-1].strip()
values[column_name] = value
new_values = values.copy()
data.append(new_values)
workbook = openpyxl.Workbook()
worksheet = workbook.active
worksheet.append(row)
workbook.save("output.xlsx")
this one?
Yes, that's the modified script that extracts the [Model Name] value even if it contains multiple
comma-separated values.