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
  convert numbers into list lokesh 1 382 Jun-03-2021, 06:37 AM
Last Post: menator01
  Adding a list to Python Emailing Script Cknutson575 4 531 Feb-18-2021, 09:13 AM
Last Post: buran
  How to skip to a different line while importing a different script kat_gamer 2 476 Feb-03-2021, 04:10 AM
Last Post: deanhystad
  adding numbers in a list Nickd12 2 558 Jan-15-2021, 12:46 PM
Last Post: Serafim
  Print max numbers in a list jimmoriarty 1 526 Sep-25-2020, 07:29 AM
Last Post: DPaul
  Importing python data to Textfile or CSV yanDvator 0 497 Aug-02-2020, 06:58 AM
Last Post: yanDvator
  importing a CSV file into Python russoj5 1 1,170 Aug-02-2020, 12:03 AM
Last Post: scidam
  Summing a list of numbers Oldman45 6 819 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 5,934 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 1,377 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur

Forum Jump:

User Panel Messages

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