简体   繁体   中英

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. But since Pandas is reading it as a string, I am not able to use it for processing further. 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

The below shown data is what I have in my CSV and trying to read this from 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

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

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. 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. 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.

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:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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