Mr Jos Posted November 8, 2020 Posted November 8, 2020 (edited) 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. 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 November 8, 2020 by Mr Jos grammar Quote
Lok24 Posted November 8, 2020 Posted November 8, 2020 (edited) 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 November 8, 2020 by Lok24 Quote
Mr Jos Posted November 8, 2020 Author Posted November 8, 2020 (edited) 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: 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 November 8, 2020 by Mr Jos Quote
Lok24 Posted November 8, 2020 Posted November 8, 2020 What happens if you add the \n only in line 93 ? (and not in line 85, the print command)? Quote
Mr Jos Posted November 8, 2020 Author Posted November 8, 2020 (edited) 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 November 8, 2020 by Mr Jos Quote
Lok24 Posted November 8, 2020 Posted November 8, 2020 yes, because you have the \n in "write" and and automatic \n in "print" Just found that to remove the \n while "read" https://stackoverflow.com/questions/15233340/getting-rid-of-n-when-using-readlines Or just read the complete file without loop....... HTH Quote
Mr Jos Posted November 8, 2020 Author Posted November 8, 2020 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. Full working High Bay Warehouse coming soon :D Quote
David Lechner Posted November 9, 2020 Posted November 9, 2020 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) Quote
Mr Jos Posted November 9, 2020 Author Posted November 9, 2020 (edited) 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. 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 November 9, 2020 by Mr Jos grammar Quote
Mr Jos Posted November 13, 2020 Author Posted November 13, 2020 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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.