work on parsing text file entry into json
This commit is contained in:
parent
ca2f0fdc0e
commit
38c3d42117
13
main.gl
Normal file
13
main.gl
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
2023-05-21 Opening balance sheet
|
||||||
|
Assets:Cash:Huntington 43568.25
|
||||||
|
Liabilities:Credit:Discover -3692.25
|
||||||
|
Liabilities:Credit:Chase -1176.50
|
||||||
|
Liabilities:Credit:Lowes -100.00
|
||||||
|
Liabilities:Credit:Kohls -85.49
|
||||||
|
Assets:401k:Fidelity 13274.58
|
||||||
|
Assets:401k:PrincipalPPG 24383.25
|
||||||
|
Assets:401k:PrincipalHC 94470.35
|
||||||
|
Assets:401k:FidelityMasco 13274.58
|
||||||
|
Assets:401k:STRS 10000.00
|
||||||
|
|
||||||
|
|
48
next.js
Normal file
48
next.js
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
const fs = require('fs');
|
||||||
|
|
||||||
|
function parseJournalEntries(filePath) {
|
||||||
|
const entries = [];
|
||||||
|
let currentEntry = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const data = fs.readFileSync(filePath, 'utf8');
|
||||||
|
const lines = data.split('\n');
|
||||||
|
|
||||||
|
for (let line of lines) {
|
||||||
|
//line = line.trim(); // Trim leading/trailing whitespace
|
||||||
|
|
||||||
|
const regex = /^(\d{4}-\d{2}-\d{2})\s(.*)$/;
|
||||||
|
const match = line.match(regex);
|
||||||
|
|
||||||
|
if (match) {
|
||||||
|
const date = match[1];
|
||||||
|
const descr = match[2];
|
||||||
|
currentEntry = { entries: [] };
|
||||||
|
currentEntry.entries.push({ date, descr });
|
||||||
|
entries.push(currentEntry);
|
||||||
|
} else {
|
||||||
|
const regex = /[^\s+]([\w\d:.]+)/g;
|
||||||
|
const match = line.match(regex);
|
||||||
|
|
||||||
|
if (match) {
|
||||||
|
const acct = match[0];
|
||||||
|
const amount = match[1];
|
||||||
|
currentEntry.entries.push({ acct, amount });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error reading the file:', error);
|
||||||
|
}
|
||||||
|
|
||||||
|
return entries;
|
||||||
|
}
|
||||||
|
|
||||||
|
function convertToJson(entries) {
|
||||||
|
return JSON.stringify(entries, null, 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
const journalEntries = parseJournalEntries('main.gl');
|
||||||
|
const jsonData = convertToJson(journalEntries);
|
||||||
|
console.log(jsonData);
|
||||||
|
|
33
next.py
Normal file
33
next.py
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
import json
|
||||||
|
|
||||||
|
def parse_journal_entries(file_path):
|
||||||
|
entries = []
|
||||||
|
current_entry = None
|
||||||
|
|
||||||
|
with open(file_path, 'r') as file:
|
||||||
|
for line in file:
|
||||||
|
line = line.strip()
|
||||||
|
if line.startswith('│'):
|
||||||
|
account, amount = line[1:].split()
|
||||||
|
current_entry['entries'].append({
|
||||||
|
'account': account,
|
||||||
|
'amount': float(amount)
|
||||||
|
})
|
||||||
|
elif line:
|
||||||
|
if current_entry is not None:
|
||||||
|
entries.append(current_entry)
|
||||||
|
current_entry = {'date': line, 'entries': []}
|
||||||
|
|
||||||
|
if current_entry is not None:
|
||||||
|
entries.append(current_entry)
|
||||||
|
|
||||||
|
return entries
|
||||||
|
|
||||||
|
def convert_to_json(entries):
|
||||||
|
return json.dumps(entries, indent=4)
|
||||||
|
|
||||||
|
file_path = 'main.gl'
|
||||||
|
journal_entries = parse_journal_entries(file_path)
|
||||||
|
json_data = convert_to_json(journal_entries)
|
||||||
|
print(json_data)
|
||||||
|
|
50
parse.js
Normal file
50
parse.js
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
const fs = require('fs');
|
||||||
|
|
||||||
|
function parseTextFile(filePath) {
|
||||||
|
const content = fs.readFileSync(filePath, 'utf-8');
|
||||||
|
const lines = content.split('\n');
|
||||||
|
const entries = [];
|
||||||
|
let currentEntry = {};
|
||||||
|
|
||||||
|
for (let i = 0; i < lines.length; i++) {
|
||||||
|
const line = lines[i].trim();
|
||||||
|
|
||||||
|
if (line !== '') {
|
||||||
|
const parts = line.split(/\s+/);
|
||||||
|
|
||||||
|
if (parts.length === 1) {
|
||||||
|
// New entry detected, push the current entry to the entries array
|
||||||
|
if (Object.keys(currentEntry).length > 0) {
|
||||||
|
entries.push(currentEntry);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a new entry object
|
||||||
|
currentEntry = {
|
||||||
|
header: line,
|
||||||
|
details: [],
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
const account = parts[0];
|
||||||
|
const amount = parseFloat(parts[1]);
|
||||||
|
|
||||||
|
currentEntry.details.push({
|
||||||
|
account,
|
||||||
|
amount,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Push the last entry to the entries array
|
||||||
|
if (Object.keys(currentEntry).length > 0) {
|
||||||
|
entries.push(currentEntry);
|
||||||
|
}
|
||||||
|
|
||||||
|
return entries;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Usage: Provide the path to your text file here
|
||||||
|
const filePath = 'main.gl';
|
||||||
|
const entries = parseTextFile(filePath);
|
||||||
|
console.log(entries);
|
||||||
|
|
44
parse.py
Normal file
44
parse.py
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
import json
|
||||||
|
|
||||||
|
def parse_journal_entries(file_path):
|
||||||
|
entries = []
|
||||||
|
current_entry = {}
|
||||||
|
|
||||||
|
with open(file_path, 'r') as file:
|
||||||
|
for line in file:
|
||||||
|
line = line.strip()
|
||||||
|
if line.startswith('│'):
|
||||||
|
account, amount = line[1:].split()
|
||||||
|
current_entry['entries'].append({
|
||||||
|
'account': account,
|
||||||
|
'amount': float(amount)
|
||||||
|
})
|
||||||
|
elif line:
|
||||||
|
if current_entry:
|
||||||
|
entries.append(current_entry)
|
||||||
|
current_entry = {'date': line, 'entries': []}
|
||||||
|
|
||||||
|
if current_entry:
|
||||||
|
entries.append(current_entry)
|
||||||
|
|
||||||
|
return entries
|
||||||
|
|
||||||
|
def convert_to_json(entries):
|
||||||
|
formatted_entries = []
|
||||||
|
for entry in entries:
|
||||||
|
formatted_entry = {
|
||||||
|
'date': entry['date'],
|
||||||
|
'entries': []
|
||||||
|
}
|
||||||
|
for sub_entry in entry['entries']:
|
||||||
|
formatted_entry['entries'].append({
|
||||||
|
sub_entry['account']: sub_entry['amount']
|
||||||
|
})
|
||||||
|
formatted_entries.append(formatted_entry)
|
||||||
|
return json.dumps(formatted_entries, indent=4)
|
||||||
|
|
||||||
|
file_path = 'main.gl'
|
||||||
|
journal_entries = parse_journal_entries(file_path)
|
||||||
|
json_data = convert_to_json(journal_entries)
|
||||||
|
print(json_data)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user