简体   繁体   中英

Reading csv file in python and using the values as an command matrix

Hey guys I have a python program that has a predefined matrix that connects to a box over a socket connections, sends the commands from the matrix, checks the responses with the predefined responses and outputs to a csv file.

  command_matrix = [

 # RSA:FREQ:CENT
    # Lower Range Pass
        ["RSA:FREQ:CENT 9000" , "Success" , "RSA:FREQ:CENT?" , "9000"]
]

would be one part of the command matrix with a csv output of-

RSA:FREQ:CENT 9000, Success,    Success,        RSA:FREQ:CENT?, 9000,   9000

With the program instead of editing the matrix each time for differnet tests we would like to use a csv file that a user creates, read that, and replace that with the command matrix.

The format for the csv file that the user will create will look like this-

RSA:FREQ:CENT 9000, Success,    RSA:FREQ:CENT?, 9000

Where we need each column a separate part of a matrix.

I cant seem to find much information on how to read a file and convert the values. I only gotten as far as reading the file and just displaying the information in the output.

If you want to read a comma-separated file to a list in python you could do something similar to this. Note that it's reading the file from standard input and only the first line.

import sys
row = [x.strip() for x in sys.stdin.readline().split(',')]
command_matrix = [row]

I don't see the point of why you posted that python code, it doesn't really make any sense with regards to your question.

If you want to read the contents of a csv file command-matrix.csv into the variable command_matrix , you can do something like the following:

commands_csv = open("command-matrix.csv")
command_matrix = []
for line in commands_csv:
    command_matrix.append(line.strip().split(','))

Python can loop over the lines in a file, so this will grab each line successively from the file until the end is reached, and append a row to the end of your matrix for each line read. The row is obtained by using the python built-ins strip and split , which respectively trim leading and trailing whitespace, and split a string into a list of strings based on a delimiter (in this case , ). The strip call is necessary, as otherwise you will end up with newlines at the end of the last cell of each row in your matrix.

You can get all this and more from the Python tutorial and reference (links are to Python 2.x documentation, documentation for Python 3.x is available as well). This is where I looked up the information before answering. In general, the python documentation is quite easy to navigate and read, IMO, and the tutorial quickly brings you up to speed on basic tasks. It's worth having a look there whenever you're not sure how to do something.

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