Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Array
#1
Hi, I want to print the sample values to a csv file.
The code below does create a file and prints to it.

Though I want the "value" part of the writerow to an actual sample value.

Blank1, DateTime, Blank2, SampleValue1
Blank1, DateTime, Blank2, SampleValue2
Blank1, DateTime, Blank2, SampleValue3 etc till end of sample value's

Would I use an array or list and step through a for loop.
Thanks,

import struct
import time
import csv
     
# Get Data out of Binary File
def readdata(filename):
    '''Read data from a file.  Data count is in the header'''
    with open(filename, "rb") as file:
        buffer = file.read(20)
        size, variation, objectType, pointIndex, startTime, sampleRate, count = struct.unpack('>IBBHiII', buffer)
        print(size, variation, objectType, startTime, sampleRate, count)
          
        buffer = file.read(4 * count)
        values = struct.unpack(f'={count}I', buffer)
        return values
 
print(readdata('XXXXXXXXXXX'))
 
# Create and Write to CSV File
with open('XXXXXXXXXXX.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerow(["Blank1", "DateTime", "Blank2", "Value"])
Reply
#2
Any iterrable collection should work.

And write shorter examples to demonstrate your question. All you needed for this questions were the last four lines.

And why are you asking? Did a list work? Did it fail? Have some confidence in yourself, try things, learn.
Reply
#3
Hi, sorry about the long length of code.
However, I feel I need to show in full to describe what I am about.
The code below works. However, surely there is a more elegant way.
I tried to use a for loop. But had no success.
I don't really want to type out line by line.
I may need to break the one function in to two functions, but for now.
Thanks,

import struct
import time
import csv
    
# Get Data out of Binary File
def readdata(filename,i):
    '''Read data from a file.  Data count is in the header'''
    with open(filename, "rb") as file:
        buffer = file.read(20)
        size, variation, objectType, pointIndex, startTime, sampleRate, count = struct.unpack('>IBBHiII', buffer)
        print(size, variation, objectType, startTime, sampleRate, count)
         
        count = count + 20 
        buffer = file.read(4 * (count))
        values = struct.unpack(f'={count}f', buffer)
        return values[i]

print(readdata('XXXXXXX',6))

#numbers = [6, 7, 8, 9, 10, 11]
#for val in numbers:
# Create and Write to CSV File
with open('XXXXXXX.csv', 'w', newline='') as file:
    
    
    
    
    writer = csv.writer(file)
    writer.writerow(["Blank1", "DateTime", "Blank2", readdata('XXXXXXX',6)])
    writer.writerow(["Blank1", "DateTime", "Blank2", readdata('XXXXXXX',7)])
    writer.writerow(["Blank1", "DateTime", "Blank2", readdata('XXXXXXX',8)])
    writer.writerow(["Blank1", "DateTime", "Blank2", readdata('XXXXXXX',9)])
    writer.writerow(["Blank1", "DateTime", "Blank2", readdata('XXXXXXX',10)])
    writer.writerow(["Blank1", "DateTime", "Blank2", readdata('XXXXXXX',11)])
    writer.writerow(["Blank1", "DateTime", "Blank2", readdata('XXXXXXX',12)])
Reply


Forum Jump:

User Panel Messages

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