Posts: 4
Threads: 1
Joined: Aug 2020
I have created in my PYTHON directory a file named movedata containing the following list of numbers 100, -100, 200, -200, 200 with the aim of importing the data into a file called list such that I can sequentially substitute the numbers into a “for” loop from which a serial write sends the individual numbers to /dev/ttyUSB0.
The for loop works if in the python loop I include the line below
list = [100, -100, 200, -200, 200 ]
If I import the data with
with open(‘movedata’,’r’) as f:
m = f.read()
print(m)
I get
100 -100 200 -200, 200
but if make list = [m] I then get
['100 -100 200 -200, 200\n'] and not [100 -100 200 -200, 200]
All my attempts to rid the contents of the brackets [] of the single quotes and \n have been unsuccessful.
Posts: 1,583
Threads: 3
Joined: Mar 2020
You've read the data into a (single) string. The next things you should do are:
- remove the newline that was read at the end of the line (look at
rstrip() )
- cut it into parts (you'll want to look at
split() ). Would probably be a little simpler if you only have spaces or only have commas between the numbers.
Posts: 4
Threads: 1
Joined: Aug 2020
Thank you Bowlofred. In the interim between posting my problem I tried putting square brackets around my list in the file to be imported. If I then used
with open('movedata', 'r') as f:
d= f.readline()
list = d
print(list) then gave me [200, -200 etc ]
This looked good except there is other invisible stuff between the brackets
Using the python "length" facility the list, if entered from the keyboard is 5 which is correct but length of the imported list was 25 !!
Perhaps you might know why
Posts: 1,583
Threads: 3
Joined: Mar 2020
readline() returns a string of characters.
You're probably just printing out that long string. When you ask a string for its length, it reports the number of characters inside.
>>> s = "20 40 80 200 250"
>>> len(s)
16 split() will break it into separate strings (default it breaks on whitespace).
>>> s.split()
['20', '40', '80', '200', '250']
>>> len(s.split())
5
Posts: 4
Threads: 1
Joined: Aug 2020
Thank you again Bolofred. The split operation seems to have done the trick with len returning 5. I have attempted to put the 5 numbers into a list with
list = [s].
When s is printed we get is 200, -200, 200. -200, 200 which is what we want but with list = [s], print(list) gives ['200, -200, 200. -200, 200'\n] and len is 1. Looks like I have muddied the waters.
Posts: 10
Threads: 0
Joined: Jul 2020
Quote:print(list) gives ['200, -200, 200. -200, 200'\n]
I think that the single ' at the beginning just after [ and at the end just before \n means that it is a string.That might be why you are getting the ,,len,, of 1.
Posts: 7,320
Threads: 123
Joined: Sep 2016
Aug-21-2020, 04:57 PM
(This post was last modified: Aug-21-2020, 04:57 PM by snippsat.)
(Aug-21-2020, 07:59 AM)barrypyth Wrote: Thank you again Bolofred. The split operation seems to have done the trick with len returning 5. I have attempted to put the 5 numbers into a list with
list = [s]. Read and split on , in one go,then have a list with with string,than can also do one more step with int() to get integers if needed.
with open('movedata.txt') as f:
lst = f.read().split(', ') Test.
>>> lst
['100', '-100', '200', '-200', '200']
>>> [int(i) for i in lst]
[100, -100, 200, -200, 200]
Posts: 8,160
Threads: 160
Joined: Sep 2016
As always, better design will make your life easier.
to somewhat simplify your code, you can have the numbers one at a line
you can have them as JSON file, which will simplify both reading and converting to integers.
Posts: 4
Threads: 1
Joined: Aug 2020
To Bowlofred,Intr0spective, Buran and Snippsat. I thank you all for trying to help this newby of many years. I tried your various solutions with disappointing outcomes. I can't exclude that the possibility that I could have made errors in applying your offered "solutions" to my problem. But there are more than one ways of solving problems usually and last night tried using numpy and had some success. It had seemed to me that if the list imported into my stepper motor program was identical to one of comma separated numbers within square brackets typed into the script it all should work. While I am delighted to have a solution albeit its klunky appearance, I am left with the observation that the numpy list imported into the script has no commas between the numbers!
|