Certainly! Below is Python code that reads the given data, parses it, and writes it to an Excel file using the openpyxl library. Similarly, I'll provide Dart code that does the same, using the excel package.
Python Code:
import openpyxl
# The data provided
data = '''
Biotrue Astigmatism Cabinet 108 packs
Cyl -0.75 Cyl -0.75 Cyl -1.25 Cyl -1.25 Cyl -1.75 Cyl -1.75 Cyl -2.25 Cyl --2.25 Cyl -0.75 Cyl -0.75 Cyl -1.25 Cyl -1.75
B05-0000075180N B05-0200075090N B05-0000125180N B05-0200125090N B05-0000175180N B05-0200175090N B05-0000225180N B05-0200225090N B05+0100075180N B05+0300075090N B05+0250125180N B05+0150175090N
B05-0000075090N B05-0250075180N B05-0000125090N B05-0250125180N B05-0000175090N B05-0250175180N B05-0000225090N B05-0250225180N B05+0100075090N B05+0400075180N B05+0250125090N B05+0200175180N
B05-0050075180N B05-0250075090N B05-0050125180N B05-0250125090N B05-0050175180N B05-0250175090N B05-0050225180N B05-0250225090N B05+0150075180N B05+0400075090N B05+0300125180N B05+0200175090N
Cyl -1.25
B05-0050075090N B05-0300075180N B05-0050125090N B05-0300125180N B05-0050175090N B05-0300175180N B05-0050225090N B05-0300225180N B05+0150075090N B05+0100125180N B05+0300125090N B05+0250175180N
B05-0100075180N B05-0300075090N B05-0100125180N B05-0300125090N B05-0100175180N B05-0300175090N B05-0100225180N B05-0300225180N B05+0200075180N B05+0100125090N B05+0400125180N B05+0250175090N
B05-0100075090N B05-0350075180N B05-0100125090N B05-0350125180N B05-0100175090N B05-0350175180N B05-0100225090N B05-0350225180N B05+0200075090N B05+0150125180N B05+0400125090N B05+0300175180N
Cyl -1.75
B05-0150075180N B05-0350075090N B05-0150125180N B05-0350125090N B05-0150175180N B05-0350175090N B05-0150225180N B05-0350225090N B05+0250075180N B05+0150125090N B05+0100175180N B05+0300175090N
B05-0150075090N B05-0400075180N B05-0150125090N B05-0400125180N B05-0150175090N B05-0400175180N B05-0150225090N B05-0400225180N B05+0250075090N B05+0200125180N B05+0100175090N B05+0400175180N
B05-0200075180N B05-0400075090N B05-0200125180N B05-0400125090N B05-0200175180N B05-0400175090N B05-0200225180N B05-0400225180N B05+0300075180N B05+0200125090N B05+0150175180N B05+0400175090N
'''
import re
# Split the data into lines
lines = data.strip().split('\n')
# Initialize variables
current_cyls = []
all_records = []
for line in lines:
line = line.strip()
if not line:
continue # Skip empty lines
if line.startswith('Biotrue'):
continue # Skip header line
elif line.startswith('Cyl'):
# This line contains Cyl values
# Split and store them
current_cyls = re.findall(r'Cyl\s*([-\d\.]+)', line)
else:
# This line contains codes
codes = line.split()
# If the number of codes matches the number of current_cyls, proceed
if len(codes) == len(current_cyls):
for cyl, code in zip(current_cyls, codes):
all_records.append({'Cyl': cyl, 'Code': code})
else:
# In case cyls are not updated, use the last known cyls
for code in codes:
all_records.append({'Cyl': current_cyls[0], 'Code': code})
# Write to Excel file
wb = openpyxl.Workbook()
ws = wb.active
ws.title = 'Biotrue Astigmatism Data'
# Write headers
ws.append(['Cyl', 'Code'])
# Write data
for record in all_records:
ws.append([record['Cyl'], record['Code']])
# Save the workbook
wb.save('Biotrue_Astigmatism_Data.xlsx')
Explanation:
- The code reads the data line by line.
- When it encounters a line starting with "Cyl", it extracts all the cylinder values using a regular expression.
- For subsequent lines, it expects codes which correspond to the cylinder values.
- It pairs each code with its corresponding cylinder value and stores them in a list of dictionaries.
- Finally, it writes the data into an Excel file using
openpyxl.
Dart Code:
import 'dart:io';
import 'package:excel/excel.dart';
void main() {
String data = '''
Biotrue Astigmatism Cabinet 108 packs
Cyl -0.75 Cyl -0.75 Cyl -1.25 Cyl -1.25 Cyl -1.75 Cyl -1.75 Cyl -2.25 Cyl --2.25 Cyl -0.75 Cyl -0.75 Cyl -1.25 Cyl -1.75
B05-0000075180N B05-0200075090N B05-0000125180N B05-0200125090N B05-0000175180N B05-0200175090N B05-0000225180N B05-0200225090N B05+0100075180N B05+0300075090N B05+0250125180N B05+0150175090N
B05-0000075090N B05-0250075180N B05-0000125090N B05-0250125180N B05-0000175090N B05-0250175180N B05-0000225090N B05-0250225180N B05+0100075090N B05+0400075180N B05+0250125090N B05+0200175180N
B05-0050075180N B05-0250075090N B05-0050125180N B05-0250125090N B05-0050175180N B05-0250175090N B05-0050225180N B05-0250225090N B05+0150075180N B05+0400075090N B05+0300125180N B05+0200175090N
Cyl -1.25
B05-0050075090N B05-0300075180N B05-0050125090N B05-0300125180N B05-0050175090N B05-0300175180N B05-0050225090N B05-0300225180N B05+0150075090N B05+0100125180N B05+0300125090N B05+0250175180N
B05-0100075180N B05-0300075090N B05-0100125180N B05-0300125090N B05-0100175180N B05-0300175180N B05-0100225180N B05-0300225090N B05+0200075180N B05+0100125090N B05+0400125180N B05+0250175090N
B05-0100075090N B05-0350075180N B05-0100125090N B05-0350125180N B05-0100175090N B05-0350175180N B05-0100225090N B05-0350225180N B05+0200075090N B05+0150125180N B05+0400125090N B05+0300175180N
Cyl -1.75
B05-0150075180N B05-0350075090N B05-0150125180N B05-0350125090N B05-0150175180N B05-0350175090N B05-0150225180N B05-0350225090N B05+0250075180N B05+0150125090N B05+0100175180N B05+0300175090N
B05-0150075090N B05-0400075180N B05-0150125090N B05-0400125180N B05-0150175090N B05-0400175180N B05-0150225090N B05-0400225180N B05+0250075090N B05+0200125180N B05+0100175090N B05+0400175180N
B05-0200075180N B05-0400075090N B05-0200125180N B05-0400125090N B05-0200175180N B05-0400175090N B05-0200225180N B05-0400225090N B05+0300075180N B05+0200125090N B05+0150175180N B05+0400175090N
''';
List<String> lines = data.trim().split('\n');
List<String> currentCyls = [];
List<Map<String, String>> allRecords = [];
RegExp cylPattern = RegExp(r'Cyl\s*([-\d\.]+)');
for (var line in lines) {
line = line.trim();
if (line.isEmpty) continue;
if (line.startsWith('Biotrue')) {
continue; // Skip header
} else if (line.startsWith('Cyl')) {
// Extract cylinder values
currentCyls = cylPattern
.allMatches(line)
.map((match) => match.group(1)!)
.toList();
} else {
// Process codes
List<String> codes = line.split(RegExp(r'\s+'));
if (codes.length == currentCyls.length) {
for (int i = 0; i < codes.length; i++) {
allRecords.add({'Cyl': currentCyls[i], 'Code': codes[i]});
}
} else {
// If cylinder values don't match, use the last known cylinders
for (var code in codes) {
allRecords.add({'Cyl': currentCyls.isNotEmpty ? currentCyls[0] : '', 'Code': code});
}
}
}
}
var excel = Excel.createExcel();
Sheet sheetObject = excel['Biotrue Astigmatism Data'];
// Add headers
sheetObject.appendRow(['Cyl', 'Code']);
// Add data
for (var record in allRecords) {
sheetObject.appendRow([record['Cyl'], record['Code']]);
}
// Save the file
var fileBytes = excel.encode();
File('Biotrue_Astigmatism_Data.xlsx')
..createSync(recursive: true)
..writeAsBytesSync(fileBytes!);
}
Explanation:
- The Dart code follows a similar logic to the Python code.
- It reads and processes the data line by line.
- Regular expressions are used to extract cylinder values.
- The
excel package is used to create and manipulate Excel files.
- The data is written to an Excel file named 'Biotrue_Astigmatism_Data.xlsx'.
Note: To run the Dart code, you need to have the excel package installed. You can add it to your pubspec.yaml file:
dependencies:
excel: ^1.1.5
Then run pub get to install the package.
Output:
Both scripts will produce an Excel file named Biotrue_Astigmatism_Data.xlsx containing two columns: 'Cyl' and 'Code', with all the data properly organized.
Feel free to run these scripts with your data. If you have any questions or need further assistance, please let me know!