Python Forum
Problem with bracket in my output - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Problem with bracket in my output (/thread-7073.html)



Problem with bracket in my output - Steffenwolt - Dec-20-2017

Hi all,
I'm a complete novice in programming in general and Python in particular and need some help every now and then hence my subscription to this forum.
The objective of my project is to collect information like temperatures, current, voltages, etc. and store them in a file (like csv) for analysis. I use a Raspi modem 3B with Python3.
So i have constructed this program from what i found on the web and thought up myself.
It can probably improved upon greatly but there is one problem i failed to solve sofar.

import time
from time import strftime
import csv

# Import SPI library (for hardware SPI) and MCP3008 library.

import Adafruit_GPIO.SPI as SPI
from Adafruit_Python_MCP3008 import Adafruit_MCP3008

# Software SPI configuration: is not used.

# CLK  = 18
# MISO = 23
# MOSI = 24
# CS   = 25
# mcp = Adafruit_MCP3008.MCP3008(clk=CLK, cs=CS, miso=MISO, mosi=MOSI)

# Hardware SPI configuration:

SPI_PORT   = 0
SPI_DEVICE = 0
mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE))


print('Reading MCP3008 values, press Ctrl-C to quit...')

# Print nice channel column headers.

print('| {0:>4} | {1:>4} | {2:>4} | {3:>4} | {4:>4} | {5:>4} | {6:>4} | {7:>4} |'.format(*range(8)))
print('-' * 57)

# Main program loop.

while True:
    # Read all the ADC channel values in a list.
    values = [0]*8
    for i in range(8):
        # The read_adc function will get the value of the specified channel (0-7).
        values[i] = mcp.read_adc(i)
    # Print the ADC values.
    print('| {0:>4} | {1:>4} | {2:>4} | {3:>4} | {4:>4} | {5:>4} | {6:>4} | {7:>4} |'.format(*values))
    print (values)

    # Extract date and time from datetime
    datetime = strftime("%d%m%y%H%M%S")
    
    # Store all data in data, 0-6 is date and 6-12 is time, values are MCP values
    # Defione the .csv file
    data = [datetime[0:6], datetime[6:12], values]
    csvfile = '/media/pi/Seagate Portable' + '.csv'
    print (data)

    # Open de csv file en schrijf
    with open(csvfile, 'a') as output:
        writer = csv.writer(output, delimiter=';', lineterminator='\n')
        writer.writerow(data)


    # Pause for two seconds.
    time.sleep(15)
The csv output file gives me the date and the time in separate cells and the values enclosed in [])in a cell rather than in 8 separate cells

Output:
201217 105603 [1003, 225, 0, 0, 0, 0, 0, 0] 201217 105605 [1003, 230, 0, 0, 0, 0, 0, 0] 201217 105607 [1002, 231, 0, 0, 0, 0, 0, 0] 201217 105609 [1004, 230, 0, 0, 0, 0, 0, 0] 201217 105611 [1001, 227, 0, 0, 0, 0, 0, 0]
There must be a better way to do this but i need some help to get me going.
Suggestions for improvement of the coding are much appreciated.


RE: Problem with bracket in my output - squenson - Dec-20-2017

The line 49 defines what the content of the csv file will be:
data = [datetime[0:6], datetime[6:12], values]
It creates three cells, the third one being a list of 8 values, exactly what the output represents.

In order to get the 8 values in separated cells, you just need to add the two lists together, so the line 49 becomes:
data = [datetime[0:6], datetime[6:12]] + values