我有一个包含以下内容的文件:
X1, y1,50
X1, y2,20
X1, y3,30
X1, y4,40
X1, y1,70
X1, y2,40
X1, y3,60
X1, y4,80
X3, y1,100
X3, y2,200
X3, y3,300
X3, y4,400
X3, y1,500
X3, y2,600
X3, y3,700
X3, y4,800
X3, xx, d
如何从以下结构中的上述数据创建列表:
[[X1,[50,20,30,40],[-1],[70,40,60,80],[-1]],[X2,[100,200,300,400],[-1], [500,600,700,800],[+1]]]
第一个值存储传感器编号Ex:X1,X3,X2,…..
(无序列)
每个传感器在4个不同的行中发送4个不同的变量及其值(除了一个案例:最后一个案例):y1=50 y2=20,y3=30,y4=40
值-1 / + 1取决于y4后面的下一行:
案例1:
如果下一行包含相同的传感器编号(例如:X1),则变量y1则为-1
案例2:
如果下一行包含不同的传感器编号(例如:X2),则变量y1则为-1
案例3:
如果下一行包含相同的传感器编号(例如:X1),则变量xx则为+1
答案 0 :(得分:0)
我是编程的新手,所以我只是为了自己学习。这可能不是一个非常有效的解决方案,但是嘿 - 这是: - )
import csv # import csv-module
with open("foo.txt", "r") as csvfile: # Open file as csvfile
foo = csv.reader(csvfile) # Read as comma-separeted file
raw_data = []
for row in foo:
raw_data.append(row)
def get_sensor_data(sensor_name):
"""This function iterates through raw_data looking for a unique sensor name.
It makes and packs lists in lists as per your rules on StackOverflow
...and returns the correct format for the data of one sensor."""
sensor_data = [sensor_name]
sensor_data_block = []
for line in raw_data:
if line[1] == " xx":
sensor_data_block.append([1])
if line[0] == sensor_name and line[1] == " y4":
sensor_data_block.append(line[2])
sensor_data.append(sensor_data_block)
sensor_data.append([-1])
sensor_data_block = []
if line[0] == sensor_name and line[1] != " xx" and line[1] != " y4":
sensor_data_block.append(line[2])
return sensor_data
def make_data_object():
"""This function identifies unique sensor-names in the raw data.
It uses those names to call the get_sensor_data-function.
The lists returned from the call are stored in the data_object list. """
sensor_list = []
data_object = []
for line in raw_data:
# Identifying unique sensor names, and calling the get_sensor_data function to get its data
try:
sensor_list.index(line[0])
except:
sensor_list.append(line[0])
for i in sensor_list:
data = get_sensor_data(i)
data_object.append(data)
return data_object
print(make_data_object())