如何存储与提取responseGetCsv返回响应中的单个值?
Hey there! Let's walk through how to handle this responseGetCsv response—storing the data and pulling out individual values is straightforward once we break it down.
Step 1: Isolate the CSV Data from the Raw Response
First, your raw response mixes the HTTP status, actual CSV content, and response headers. We need to extract just the CSV part first. Looking at your response, the CSV starts right after 200 OK, and ends right before ,{Server=.
Here's how to do that in Python (the logic translates easily to other languages too):
raw_response = "<200 OK,Device CustomIPv6 Address 4,Version,Request URL Query,Device CustomIPv6 Address 1,Destination Identity Phone Home,Device Action,Crypto Signature,Old File Size,Flex Date 1 Label,Category Significance,Old File Hash ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational, ,1,,,,,,,,/Informational,,{Server=[nginx/1.12.2], Date=[Mon, 09 Dec 2019 09:14:03 GMT], Content-Type=[application/octet-stream], Transfer-Encoding=[chunked], Connection=[keep-alive], X-Powered-By=[Sails ], Cache-Control=[private, no-cache, no-store, must-revalidate], Expires=[-1], Pragma=[no-cache], X-Frame-Options=[SAMEORIGIN], Access-Control-Allow-Origin=[], Access-Control-Allow-Credentials=[], Access-Control-Allow-Methods=[], Access-Control-Allow-Headers=[], Access-Control-Expose-Headers=[], Content-Disposition=[attachment; filename="testGetCsv_09/12/2019 14:43:43.csv";], Strict-Transport-Security=[max-age=15724800; includeSubDomains;]}>" # Extract the CSV segment csv_content = raw_response.split("200 OK,")[1].split(",{Server=")[0].rstrip(',')
Step 2: Store the CSV Data
You have two main options here—save it to a file for later use, or parse it into structured memory data for immediate access:
Option A: Save to a Local CSV File
If you want to keep the data persistent, write it to a file:
with open('device_data.csv', 'w', newline='') as csv_file: csv_file.write(csv_content)
Option B: Parse into Structured Data (for quick value extraction)
For easier access to individual values, parse the CSV into a list of rows or dictionaries. Using Python's built-in csv module:
import csv from io import StringIO # Parse into a list of rows (first row is headers) csv_reader = csv.reader(StringIO(csv_content)) data_rows = list(csv_reader) # Or parse into a list of dictionaries (maps headers to row values) headers = data_rows[0] data_dicts = [dict(zip(headers, row)) for row in data_rows[1:]]
Step 3: Extract Individual Values
Now that you have structured data, pulling out specific values is simple:
If using row lists:
Each row is a list where indexes match the header positions. For example, to get the Version value from the first data row:
# Version is the 2nd header (index 1, since we start at 0) first_row_version = data_rows[1][1] print(f"First data row Version: {first_row_version}")
If using dictionaries:
You can access values directly by header name, which is more readable. For example, get the Category Significance from the 5th data row:
fifth_row_category = data_dicts[4]["Category Significance"] print(f"Fifth data row Category Significance: {fifth_row_category}")
A quick note: The empty fields in your CSV (those consecutive commas) will show up as empty strings in the parsed data—you can handle those with simple checks if needed.
内容的提问来源于stack exchange,提问作者Veerendra

