0

I am using Circuitpython for my microcontroller. I am writing whatever my microcontroller do when internet is gone to text file like this:

[{'name': 'exampleA', 'time': '2022-03-15 15:12:55.12541', 'id': '5456431', 'howmany': 2, 'character':None}]

exampleA can repeat in this text file, I always need to check last exampleA's values. So I wrote this;

string = exampleA  # it will be taken from user.
file = open("example.txt")
for line in reversed(file.readlines()):
    if string in line:
        print(len(file.read()))
        print(string)

When I found exampleA, how can I take exampleA's time too? Because for example if microcontroller do something with exampleA, it shouldn't do things for at least 3 hour. So I need to take last exampleA's time.

  • Is each line a valid Python `repr`? (Is there a reason you wrap the dictionary in a list? Perhaps use JSON in your file instead of Python literals.) – tripleee Mar 19 '22 at 11:00
  • Once you have read all the data into memory, you can no longer `file.read()` unless you close and reopen the file, or rewind it with `seek`. But the data is already in memory; examine the variable you are looping over instead of trying to read the file some more. Voting to close as typo. – tripleee Mar 19 '22 at 11:03
  • I need to read this file when internet is gone. If I have internet, I can take datas from api but when internet is gone, microcontroller have to do this by itself and when internet is connected, it needs to post this text.file to api. – Tryingtogetsome Mar 19 '22 at 11:05
  • tripleee, thank you for explanation. – Tryingtogetsome Mar 19 '22 at 11:06

0 Answers0