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
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 =

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.
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.
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
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)
split() will break it into separate strings (default it breaks on whitespace).

>>> s.split()
['20', '40', '80', '200', '250']
>>> len(s.split())
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.
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.
(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 =', ')
>>> lst
['100', '-100', '200', '-200', '200']
>>> [int(i) for i in lst]
[100, -100, 200, -200, 200]
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

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!

Possibly Related Threads…
Thread Author Replies Views Last Post
  adding numbers in a list Nickd12 2 119 Jan-15-2021, 12:46 PM
Last Post: Serafim
  Print max numbers in a list jimmoriarty 1 268 Sep-25-2020, 07:29 AM
Last Post: DPaul
  Importing python data to Textfile or CSV yanDvator 0 269 Aug-02-2020, 06:58 AM
Last Post: yanDvator
  importing a CSV file into Python russoj5 1 638 Aug-02-2020, 12:03 AM
Last Post: scidam
  Summing a list of numbers Oldman45 6 476 Jul-12-2020, 05:30 PM
Last Post: Oldman45
  How do I calculate a ratio from 2 numbers and return an equivalent list of about 1000 Pleiades 6 3,539 Jul-01-2020, 03:53 PM
Last Post: ndc85430
  How to kill a bash script running as root from a python script? jc_lafleur 4 679 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur
  Topic: “Filter numbers with a list comprehension” (PyBite #107) Drone4four 4 490 Jun-11-2020, 08:31 PM
Last Post: Drone4four
  crontab on RHEL7 not calling python script wrapped in shell script benthomson 1 508 May-28-2020, 05:27 PM
Last Post: micseydel
  importing CSV file into a OOP Class table using Python faruk61 1 456 Apr-15-2020, 12:00 PM
Last Post: faruk61

Forum Jump:

User Panel Messages

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