简体   繁体   English

如何从 python 中的文件中提取数据

[英]how to extract data from file in python

How can i plot these data in a multiline graph?我如何在多线图中 plot 这些数据?

I have tried using these code but only the first value of each line is being added to the list我尝试使用这些代码,但只有每行的第一个值被添加到列表中

For my y axis I want to get the values of the appliance multiplying the values of 0.5,0,1,0(For example for line 1,(tv value = 120) I should get 24 plots which are at对于我的 y 轴,我想得到设备的值乘以 0.5,0,1,0 的值(例如对于第 1 行,(tv 值 = 120)我应该得到 24 个图,它们位于

y axis:60 and x axis 0 y 轴:60 和 x 轴 0

y axis 0 and x axis 1 y 轴 0 和 x 轴 1

y axis 120 and x axis 2 y 轴 120 和 x 轴 2

and so on... having a total of 24 plots(My y labels will consist of values from 0 to 2200)依此类推...总共有 24 个图(我的 y 标签将由 0 到 2200 的值组成)

For my x axis I will be ploting the values of x_labels对于我的 x 轴,我将绘制 x_labels 的值

file format文件格式

Residents: 4
TV1:120:0.5,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0.5,0,0,0.5,1,1,1,0.5
Computer:320:1,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
Fridge1:250:1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
Dishwasher:500:0.5,1,1,0.5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1
Blender:700:0,0,0,0,0,0,0,0,0,0,0,0,0,0.05,0,0,0,0,0,0,0,0,0,0
Fridge2:50:1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
Coffee machine:2400:0,0,0,0,0,0,0,0,0,0.05,0.05,0,0,0,0,0,0,0,0,0,0,0,0,0
Kettle:2200:0.05,0,0,0,0,1,0,0,0.05,0,0.05,0.05,0,0,0,0,0.05,0,0,0,0,0.05,0.05,0
Freezer:140:1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
Slow cooker:300:0,0,0,0,0,0,0,0,0,0,0,0,0.5,1,1,1,1,1,0.5,0,0,0,0,0

My code我的代码

import matplotlib.pyplot as plt
import numpy as np

x = []
y = []

x_labels = ['0','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23']

fileobj = open('file.csv','r')
for line in fileobj:
    line_s = line.strip()
    Usage = [(x) for x in line_s.split(',')]
    y.append(Usage[2])




fileobj.close()

plt.plot(y ,"o--")
plt.ylabel("usage")
plt.xlabel("time")
plt.show()

import matplotlib.pyplot as plt
import numpy as np

x_labels = ['0','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23']

with open(r'C:\Users\$env:UserName\Desktop\MyScripts\file.csv', 'r') as file_obj:
    for line in file_obj:
        line = line.strip('\n')
        # print(f"{line = }")
        usage = [x for x in line.split(',')]
        # print(f"{usage = }")
        number = float(usage[0].split(':')[1])
        first = [float(usage[0].split(':')[-1])]
        remaining = [float(x) for x in usage[1:]]
        # print(f"{first = }")
        # print(f"{type(first) = }")
        # print(f"{remaining = }")
        first.extend(remaining) # * number
        res = list(map(lambda x: x * number, first))
        print(f"{res = }")

plt.plot(res, "o--")
plt.ylabel("usage")
plt.xlabel("time")
plt.show()

在此处输入图像描述

When you read the file, you're getting all data along x (time) for every device.当您读取文件时,您将获得每个设备沿 x(时间)的所有数据。 If you want plots for all devices, you can plot them as you read the data.如果您想要所有设备的绘图,您可以在读取数据时对它们进行 plot。 It appears you want the devices as labels.看来您希望设备作为标签。

fileobj = open('file.csv','r')
line = fileobj.readline()
for i, line in enumerate(fileobj):
  device, wattage, h = line.split(':')
  watts = [float(x) * float(wattage) for x in h.replace('\n','').split(',')]
  # print(device, watts, wh)
  plt.plot(watts, label=str(i+1))
  # or plt.plot(watts, label=device) # if you want device as label

plt.legend()

Welcome to SO!欢迎来到 SO!

Lets try this!让我们试试这个!

fileobj = open('file.csv','r')
first_line = True

for line in fileobj:
    if first_line:
       first_line = False
       continue
    line_s = line.strip() # line -> Slow cooker:300:0,0,0,..
    name, id, values = line.split(':') # ['Slow cooker', '300', '0,0,0']
    usage = [int(x) for x in values.split(',')] # convert values into integers
    print(usage)
    y.append(usage)

My understanding is you are trying to plot this text data.我的理解是你正在尝试 plot 这个文本数据。 Y is the usage values in the format of a list of lists. Y 是列表列表格式的使用值。

Please comment otherwise.否则请发表评论。 If you are not sure of what is happening, then use the print command as I have added in the above code.如果您不确定发生了什么,请使用我在上面代码中添加的print命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM