How to get comments in python

43 views
Skip to first unread message

ra

unread,
Jun 8, 2021, 9:29:05 AMJun 8
to Ledger
How can can I print comments e.g. "MD5Sum: 4d296..." in python?

The *working* bash equivalent is ` ledger -f file.dat csv --csv-format "%(join(note | xact.note))"`

Tried this snippet so far:

```python
import ledger

for xact in ledger.read_journal("file.dat").xacts():
for post in xact.posts():
print(post.date, post.amount, post.account)
# print(post.note) # Returns None
# print(post.__dict__) # Attempting to print all key:values...returns empty
```

Teito Klien

unread,
Jun 9, 2021, 12:54:47 AMJun 9
to ledge...@googlegroups.com
Hi Rida, 

I ran here’s a snippet to help you collect the notes attached in form of a pastebin link 

You see what’s happening is you’re trying to print the note of each line present in each transaction in the nested for loop which is why it’s returning you None for the lines which have comments missing (I’m assuming you’re marking comments using semicolons ; )

Try out the code snippet linked below 

https://pastebin.com/CpgK8iiv

It will print out all the comments in each of your journal posts , you just need to filter out the None lines and just append the ones which have a comment ,


Also if you want to check what attributes a post object has that you can play with and __dict__ shows you blank , try out the 

dir(post) command , it will show you both the functions you can call and the variables present in the object ,  to print it out simple 
Use a for loop to print the attributes 
Something like ,

for attribute in dir(post):
    print(
        attribute,
        "value=",
         getattr(post,attribute))


If you face any issue with getting the notes using the script I gave you in pastebin , let me know I’ll try to help out , I tested the code out on my ledger file and it worked pretty well

 
--

---
You received this message because you are subscribed to the Google Groups "Ledger" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ledger-cli+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ledger-cli/878s3k5vtu.fsf%40notebox.ch.

ra

unread,
Jun 19, 2021, 5:29:12 AMJun 19
to Ledger
Thank you Teito Klien. I only get 3 and 6 returned instead of the complete note. Hope the following markdown depicts the problem better:

`cat file.dat`

    2019/11/07 * foo
        ; MD5Sum: 4d2965fa2879f3c528006f2e9c3bca8d
        Expenses:Groceries                                              CHF 1.5
        Assets:Cash
    
    2019/11/07 * bar
        ; MD5Sum: 4d2965fa2879f3c528006f2e9c3bca8e
        Expenses:Groceries                                              CHF 2.5
        Assets:Cash

`cat ledger_notes.py`

    #!/usr/bin/env/python

    
    import ledger
    
    
    for xact in ledger.read_journal("file.dat").xacts():
        note = ""
        for post in xact.posts():
            if post.note !=None:
                note+=post.note+"\n"
        print(note)
        print(post.id())

    
Result:

    3
    
    6

`cat ledger_vars.py`

    import ledger
    
    
    for xact in ledger.read_journal("file.dat").xacts():
        for post in xact.posts():
            for attribute in dir(post):
                print(
                    attribute,
                    "value=",
                    getattr(post,attribute))

Result:

    Traceback (most recent call last):
      File "<stdin>", line 10, in <module>
    Boost.Python.ArgumentError: Python argument types in
        None.None(Posting)
    did not match C++ signature:
        None(supports_flags<unsigned char, unsigned char> {lvalue})


Reply all
Reply to author
Forward
0 new messages