Python Forum
Creating csv header from user-input list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating csv header from user-input list
I'm writing a script to read signals from a 32-channel pressure scanner via an Arduino. The basic operation is that the user will input a list defining the scanner's ports he/she is going to use, then specify the duration the test should run (how long will it collect data). After that, the program will send an address to the scanner via digital pins 2 through 7 on the Arduino that represent the port address in binary (eg 000000 for port 1, addressed as port 0, and 111110 for port 32, addressed as port 31).

Currently the output reads in the format [elapsed time, port #, signal], then it goes to the next port in a new line. I'd like to configure this to write to a csv in which the first row is the port numbers as identified by the user, and all subsequent rows are the signals for each port at a given elapsed time. For example:
time, 1, 2, 3, 4, 5
0.1, 0, 128, 256, 512, 1024
0.2, 128, 256, 512, 1024, 0

Would anyone be able to help me configure this?

Here's the code. It doesn't have anything to do with csv stuff right now.
from pyfirmata import Arduino, util
import time

comport = 'COM13'
sleep = 0.025

board = Arduino(comport)
a0 = board.get_pin('a:0:i')

it = util.Iterator(board)

print('Enter the port numbers you are using, separated only by spaces:')
ports = [int(x) for x in input().split()]
print('For how long do you want to take data? Enter the time in seconds:')
duration = input()
duration = int(duration)

print ('The ports used in this test are ' + str(ports))
print ('The test will run for ' + str(duration) + ' seconds')
print ('Continue? y/n')

condition = input()

length = len(ports)

for j in range(length):
    ports[j] = int(ports[j])-1

if condition=='y':
    start = time.time()
    current = 0

    while current < duration:
        for i in range(length):
            value = str(bin(ports[i])[2:].zfill(6))
            newval = list(value)
            analogval =
            analogval = analogval*1024
            current = time.time()-start
            print("%.4f" % current, ports[i], analogval)

    print('Please run the script again, and check all setup parameters carefully before entering')
Here's what the output currently looks like:
Enter the port numbers you are using, separated only by spaces: 1 2 3 4 5 6 7 For how long do you want to take data? Enter the time in seconds: 1 The ports used in this test are [1, 2, 3, 4, 5, 6, 7] The test will run for 1 seconds Continue? y/n y 0.0020 0 532.48 0.0271 1 532.48 0.0521 2 541.4912 0.0820 3 541.4912 0.1109 4 541.4912 0.1400 5 523.4688 0.1693 6 533.504 0.1993 0 531.5584
I'm also seeing I'll have to adjust the ports so it prints out starting at 1 instead of 0. That should be an easy fix that I didn't catch until typing that out just now.
the header record is just a text record with the same number of fields as the data.
The delimiter must be the same (normally ',', but other character can be used (so long as accounted for when reading later))
each line must be terminated with a newline ('\n')
you can write simply as text, example (untested):

def write_header(fp):

def write_data(time, port, signal):

def main():
   with open('myfile.csv', 'w') as fp:
       ... gather data ...
       for time, port, signal in data:
           write_data(time, port, signal)

Thanks for the help. With that I have the data going in a .csv file with a name of my choosing. I was thinking it was still an incorrect format, but after thinking about it, since the ports are being addressed sequentially (each signal has a different time stamp), it won't make any sense to have the format I was discussing earlier (one line of data from each port on the same time stamp). I'll be able to parse this file and analyze the data as it is.

I may be back here with more later, but for now I can proceed!

Many thanks.

Possibly Related Threads…
Thread Author Replies Views Last Post
  Loop back through loop based on user input, keeping previous changes loop made? hbkpancakes 2 136 Nov-21-2020, 02:35 AM
Last Post: hbkpancakes
  Creating list of lists from generator object t4keheart 1 129 Nov-13-2020, 04:59 AM
Last Post: perfringo
  Creating a dictionary from a list Inkanus 5 304 Nov-06-2020, 06:11 PM
Last Post: DeaD_EyE
  Creating a list of dictionaries while iterating pythonnewbie138 6 448 Sep-27-2020, 08:23 PM
Last Post: pythonnewbie138
  user input for multi-dimentional list without a prior iteration using input() Parshaw 6 351 Sep-22-2020, 04:46 PM
Last Post: Parshaw
  How to time a user's input Tbot1000 2 278 Aug-31-2020, 09:39 PM
Last Post: Larz60+
  converting user input to float troubles RecklessTechGuy 3 396 Aug-17-2020, 12:41 PM
Last Post: deanhystad
  Validating user input WJSwan 2 359 Jul-06-2020, 07:21 AM
Last Post: menator01
  Hi, I need help with defining user's input and applying it to code. jlmorenoc 2 453 Jun-24-2020, 02:10 PM
Last Post: pyzyx3qwerty
  Creating a list of RSSI value and send it to the server. Azuan 0 285 Jun-08-2020, 11:22 PM
Last Post: Azuan

Forum Jump:

User Panel Messages

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