Mr Jos

Micropython can it list to file, and backwards?

Recommended Posts

Hey,

(I have never coded before with Python, but have been learning it last week with online reading a lot, this problem I can not find)

I changed my project from 3 bricks running EV3-G to 1 remaining on EV3-G, other 2 changed to Pybricks, micropython.

As far as moving the motors it works now perfect, better then 3x EV3-G. Bluetooth is now server(pybrick) with 2 clients(1pybrick, 1ev3-g), another big problem that got solved.

 

But: I used to make a WareHouse Managing File on 1 of the now server Pybrick. I tried to make a .txt file that is stored when the program does not run. Then it should everytime open the file and fill in a list 'online' to access and change the states.

The problem I run into is that when I try to save the 'online' list to the 'offline' .txt file, it puts all of the input on 1 line instead of each line 1 state (True or False).

Next run of the program it should load in the file, but as it's all in 1 line, there's only 1 element to put in the list, rest is blank.

50580399552_b0bb6af056_b.jpg 50580399602_226778a735_b.jpg

On the first picture it's printed as I want it, every line of print has it's own True/False (later I will use the testlist[position] to see if a position already has a box or not). But I get the last picture after starting the program with an empty testlist.

 

TL,DR:

Anyone knows how I can put several "strings" each on 1 line in a .txt file with MicroPython, taking the inputs from a list.

 

Edited by Mr Jos
grammar

Share this post


Link to post
Share on other sites

Hi,

sounds like a nice project!

As far as I know the python "write" which you use does not add a "new line" (whereas your print does!)
Try file1.write("your value \n")

 

Edited by Lok24

Share this post


Link to post
Share on other sites

I tried it before, for some odd reason it adds a blank line in between the values.

I'll try to replicate again and show it.

Edit:

50580446226_a54c8f8b34_b.jpg

Here it is, as you can see it adds the first value correct on 1 line, but then adds and empty line, then 2nd value on 3rd line, empty, etc.

Edited by Mr Jos

Share this post


Link to post
Share on other sites

What happens if you add the \n only in line 93 ?

(and not in line 85, the print command)?

Share this post


Link to post
Share on other sites

I only added it in line 93 -->  + "\n"

line 85 remained the same in all 3 pictures, it just reads value per value from the list, that works good.

Somehow the .txt writing puts all on 1 line(second picture), or puts directly an empty line in between the values(third picture).

Actually it adds and extra empty line every time I run the program starting from an empty list. (1st time 1empty, 2nd time 3empty, 4th time loads of empty lines)

Edited by Mr Jos

Share this post


Link to post
Share on other sites

Ah! Thank you, didn't know it automatically added a \n in print!

After reading in the link you gave me I managed to solve the problem, thank you! It works the same everytime I restart the program, will do some further testing as to how it works and i can make a WMS from it.

50580762002_0c8d37cb8f_b.jpg

Full working High Bay Warehouse coming soon :D

Share this post


Link to post
Share on other sites

Python tip 1:

The range function provides the next number in each iteration of the loop, so instead of

count = 0
for x in range(10):
    print(my_list[count])
    count += 1

You can write:

for x in range(10):
    print(my_list[x])

which leads to...

Python tip 2:

Lists are iterators, so instead of using

for x in range(10):
    print(my_list[x])
    

You can do this:

for item in my_list:
    print(item)

 

Python tip 3:

You can use the print function to print to a file

print("stuff", file=my_file)

This will automatically add the newline.

 

Putting this all together

with open("my-file", "w") as f:
    for item in list:
        print(item, file=f)

 

The title also mentioned backwards:

with open("my-file", "w") as f:
    for item in reversed(list):
        print(item, file=f)

 

Share this post


Link to post
Share on other sites
5 hours ago, David Lechner said:

Python tip 1: Thanks, already found this one out today

Python tip 2: Thanks, already found this one out today as well

Python tip 3: Thanks, something new learned again

You can use the print function to print to a file


print("stuff", file=my_file)

This will automatically add the newline.

 

Putting this all together


with open("my-file", "w") as f:
    for item in list:
        print(item, file=f)

 

The title also mentioned backwards:


with open("my-file", "w") as f:
    for item in reversed(list):
        print(item, file=f)

 

I cleaned it up today already after some testing and reading/learning.

50584041743_db26ecbed1_b.jpg

The backwards in the title was actually just for list --> file [104:106] , backwards = file --> list [92:93]. But as you can see on the photo I already managed to get that.

Now the program is already running full initialising all 3 brick's motors/sensors, positions of boxes on conveyors are being found. Boxes in the high bay rack are stored offline in the file correctly. Choosing automatically a free spot in the rack is being done good. Next part to program tomorrow is automatically returning boxes if in 'auto' mode.

Then the hardest part is left, to make a HMI with the 2 Python bricks. Many more reading/learning wil be done this week.

Edited by Mr Jos
grammar

Share this post


Link to post
Share on other sites

2weeks of learning Python, from 0 to ...

It's not perfect the HMI, few bugs with information updating to late on screen. But overall works well. Next will be the HMI on second screen for 'online' parameter adjustment.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.