Array - 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: Array (/thread-29453.html) |
Array - Aussie - Sep-03-2020 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"]) RE: Array - deanhystad - Sep-03-2020 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. RE: Array - Aussie - Sep-03-2020 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)]) |