简体   繁体   English

使用 object 数据类型中的 pandas 从 CSV 读取单元格

[英]Read a cell from CSV using pandas in object data type

I have been working on reading a cell through Pandas. The value in the cell is actually used to read data from a Modbus device.我一直致力于通过 Pandas 读取一个单元格。单元格中的值实际上用于从 Modbus 设备读取数据。 But since Pandas is reading it as a string, I am not able to use it for processing further.但由于 Pandas 正在将其作为字符串读取,因此我无法将其用于进一步处理。 Can anyone tell me how to NOT read the data as string type from the Pandas or what is the actual format/datatype to read the cells谁能告诉我如何不从 Pandas 读取字符串类型的数据,或者读取单元格的实际格式/数据类型是什么

The below shown data is what I have in my CSV and trying to read this from Pandas下面显示的数据是我在 CSV 中的数据,并试图从 Pandas 中读取它

client.read_holding_registers(0,unit=1)
client.read_holding_registers(1,unit=1)
client.read_holding_registers(2,unit=1)

Below is a piece of code which i am using to read the csv data下面是我用来读取 csv 数据的一段代码

file = ('MB_REGISTERS.csv')
print ("Starting to read MB CSV")
sheet1 = pds.read_csv(file)
total_rows = sheet1.shape[0]
Column_address = sheet1['Sheet1']
print (Column_address)

Below is the output seen after running the code下面是运行代码后看到的output

ModbusTcpClient(127.0.0.1:502)
Starting to read MB CSV
Printing total rows
3
0    client.read_holding_registers(0,unit=1)
1    client.read_holding_registers(1,unit=1)
2    client.read_holding_registers(2,unit=1)
Name: MB_REGISTERS, dtype: object
['client.read_holding_registers(0,unit=1)'
 'client.read_holding_registers(1,unit=1)'
 'client.read_holding_registers(2,unit=1)']
Starting to read Modbus Register Address
object

As seen above, the cells are read as it is in string format.如上所示,单元格以字符串格式读取。 When I read the same value as an array directly from python code, it is working.当我直接从 python 代码读取与数组相同的值时,它正在工作。 Below is an example for the same以下是相同的示例

k1 = np.array([client.read_holding_registers(0,unit=1),client.read_holding_registers(1,unit=1)])
print (k1)

The above lines print the result as desired here上面几行在此处打印所需的结果

[<pymodbus.register_read_message.ReadHoldingRegistersResponse object at 0x0FC1ECD0>
 <pymodbus.register_read_message.ReadHoldingRegistersResponse object at 0x0F3736D0>]

I want the pandas to read the csv in a processable format and not in string.我希望 pandas 以可处理的格式而不是字符串形式读取 csv。 I hope my question is clear.我希望我的问题很清楚。 Thanks in advance提前致谢

One of the joys/horrors (depending on your standpoint) of a scripting language like Python is that you can make up code on the fly, using the eval() function.像 Python 这样的脚本语言的乐趣/恐惧(取决于您的立场)之一是您可以使用eval() function 即时编写代码。

l = ['John','Graham','Michael']
strExpr = 'l[1]'
print(strExpr,'=',eval(strExpr))

gives a result of:给出以下结果:

l[1] = Graham

So in this case,所以在这种情况下,

l = ['client.read_holding_registers(0,unit=1)',
     'client.read_holding_registers(1,unit=1)',
     'client.read_holding_registers(2,unit=1)']

k1 = np.array([eval(reg) for reg in l])

will evaluate whatever is in the string-based expression.将评估基于字符串的表达式中的任何内容。

EDIT: Since OP only wants to loop through items once, and assuming that client is already a valid object:编辑:由于 OP 只想遍历项目一次,并假设client已经是有效的 object:

k1 = []

f = open('MB_REGISTERS.csv',mode='r')
while True:
    line = f.readline()   
    if not line:
        break
    k1.append(eval(line))

print(k1)

(NB. Extra checks might be needed for blank lines etc. Also it seems that eval() does not mind having the newline character at the end of the string) (注意。空行等可能需要额外检查。而且 eval() 似乎不介意在字符串末尾有换行符)

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

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