Python Forum
Problem with bracket in my output
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with bracket in my output
#1
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.
Reply
#2
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem in output of a snippet code akbarza 2 388 Feb-28-2024, 07:15 PM
Last Post: deanhystad
  output shape problem with np.arange alan6690 5 703 Dec-26-2023, 05:44 PM
Last Post: deanhystad
  problem in output of a function akbarza 9 1,213 Sep-29-2023, 11:13 AM
Last Post: snippsat
  Grouping Data based on 30% bracket purnima1 4 1,202 Mar-10-2023, 07:38 PM
Last Post: deanhystad
  Python Pandas Syntax problem? Wrong Output, any ideas? Gbuoy 2 937 Jan-18-2023, 10:02 PM
Last Post: snippsat
  Facing problem with Pycharm - Not getting the expected output amortal03 1 865 Sep-09-2022, 05:44 PM
Last Post: Yoriz
  single input infinite output problem Chase91 2 1,958 Sep-23-2020, 10:01 PM
Last Post: Chase91
  Output to a json file problem Netcode 3 3,748 Nov-22-2019, 01:44 AM
Last Post: Skaperen
  DHT11 output to website problem cjdock 0 1,472 Oct-01-2019, 08:29 PM
Last Post: cjdock
  How to print counter without bracket in python and sort data. phob0s 1 2,778 Jul-25-2019, 05:33 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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