I am working on a flask server which accepts .log file POSTS (basically just a text file). These files contain data resulting from invoking the command line smartctl command
smartctl -a /dev/sda
I got it working, but I used the lines numbers in the file hardcoded, this isn't optimal, as the amount of lines could differ with different hard disks. The following is part of my working code:
def parse_line(line): # For colons; device info
splitted = line.split(':')
return splitted[0], splitted[1].strip()
file_body = request.form['smartdata']
lines = file_body.split("\n")
my_data = {} # Empty dictionary object
for line in lines[4:22]: # Device info
if ":" in line:
if line.startswith("Device is:"): # Not necessary
pass
else:
key, value = parse_line(line)
my_data[key] = value
I tried searching for section headers in the .log file instead to determine what kind of splitting I should be doing;
def parse_line(line): # For colons; device info
splitted = line.split(':')
return splitted[0], splitted[1].strip()
copy = False
device_info = {}
for line in lines:
if line.strip() == "=== START OF INFORMATION SECTION ===": #start of device info
copy = True
if line.strip() == "=== START OF READ SMART DATA SECTION ===": #start of smart data section, end of device info
copy = False
if copy: #if iterating lines and passed the information section header, true and sends said line to the parse_line method
key, value = parse_line(line)
device_info[key] = value
However, I am getting the following error:
UnboundLocalError: local variable 'value' referenced before assignment
I don't quite get how I could be getting this error from the
device_info[key] = value
As I am basically doing the same thing as I was before.
Shouldn't the device_info[key] = value
line be in the same block where value
get's assigned?
def parse_line(line): # For colons; device info
splitted = line.split(':')
return splitted[0], splitted[1].strip()
copy = False
device_info = {}
for line in lines:
if line.strip() == "=== START OF INFORMATION SECTION ===": #start of device info
copy = True
if line.strip() == "=== START OF READ SMART DATA SECTION ===": #start of smart data section, end of device info
copy = False
if copy: #if iterating lines and passed the information section header, true and sends said line to the parse_line method
key, value = parse_line(line)
# Now value will always be defined.
device_info[key] = value
As some said, device_info should be indented correctly. I had to add a check to see if ":" is in line and only then assign a value to my dict, as my parse_line method is expecting a colon to split lines. The following code works.
def parse_line(line): # For colons; device info
splitted = line.split(':')
return splitted[0], splitted[1].strip()
copy = False
device_info = {}
for line in lines:
if line.strip() == "=== START OF INFORMATION SECTION ===":
copy = True
if line.strip() == "=== START OF READ SMART DATA SECTION ===":
copy = False
if copy and ":" in line: # Make sure : is in line to use parse_line
key, value = parse_line(line)
# Value will be defined
device_info[key] = value
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.