Python Forum
importing a list of numbers into python script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
importing a list of numbers into python script
#1
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.
Reply
#2
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.
Reply
#3
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
Reply
#4
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
Reply
#5
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.
Reply
#6
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.
Reply
#7
(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]
Reply
#8
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I calculate a ratio from 2 numbers and return an equivalent list of about 1000 Pleiades 8 15,440 Jan-05-2024, 08:30 PM
Last Post: sgrey
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,008 Jun-29-2023, 11:57 AM
Last Post: gologica
Question Need help for a python script to extract information from a list of files lephunghien 6 1,033 Jun-12-2023, 05:40 PM
Last Post: snippsat
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,013 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  List of random numbers astral_travel 17 2,533 Dec-02-2022, 10:37 PM
Last Post: deanhystad
  python standard way of importing library mg24 1 872 Nov-15-2022, 01:41 AM
Last Post: deanhystad
  Remove numbers from a list menator01 4 1,251 Nov-13-2022, 01:27 AM
Last Post: menator01
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,428 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  Divide a number by numbers in a list. Wallen 7 7,926 Feb-12-2022, 01:51 PM
Last Post: deanhystad
  producing numbers out of a list bouraque7878 10 3,622 Nov-12-2021, 09:13 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020