Reputation: 5
I wrote a Python script in order to extract Hard Disk information from a log file. The log file i am working with is badly formatted. Therefore i searching for all values together like this:
lineDict = dict([(line) for line in enumerate(hd.readlines())])
x = json.dumps(lineDict, indent = 1)
y = re.findall(r'Caption=.*[c-zC-Z]:|FileSystem=NTFS|Size=\d+|FreeSpace=\d+',x,re.M)
All my Free Space and Size (Total Size) values are bytes , so i have to loop through them separated from other values in order to quickly convert them to ints and later on convert those bytes values to Human readable form. (MB,GB,etc...): I was messing around with those Free Space and Total Size values in order to display the output nicely on screen , with pipe separator for Total Size and Free Space:
getBytes = re.findall(r'\d+',cropHdds)
getBytes = [int(x) for x in getBytes]
valsL = []
for vals in getBytes:
hVals = convertBytes(vals)
valsL.append(hVals)
hrFixed = json.dumps(valsL, indent = 2)
hrDisplay = replaceMany(hrFixed.strip(),[("\n",""),('"',''),("'",""),(']',''),('[','')])
a = re.sub("\\s+", "", hrDisplay)
b = textwrap.fill(a, 14)
c = b.replace(","," | ")
hrValsF = c.center(60)
Finally, i have two lists: DriveL:
['C:', 'D:', 'E:', 'F:', 'G:', 'Z:']
DriveInfoL:
['52.7GB | 119.7GB', '22.9GB | 80.0GB', '25.2GB | 50.0GB']
I wish to combine those values together , like this example below:
C:,52.7GB | 119.7GB, D: 22.9GB | 80.0GB, E:'25.2GB | 50.0GB
I have another variable which searching for the "File System Type" , i can know for a fact that i have only 3 NTFS Hard Drives (C,D,E). After searching and bashing my head on the keyboard for a very long time. I tried many sorting methods, such as convert those strings to tuple or dicts using weird list comprehensions which eventually cause a memory leak, and also tried some creative slice notations, somehow i cant get it right, therefore i seek assistance in order to overcome this obstacle, i have a feeling that i am working too hard and their must be an solution. Thanks in advance for your'e help.
Upvotes: 0
Views: 65
Reputation: 3860
This snippet will create a dictionary where the key is the drive letter and the value is the memory:
DriveL = ['C:', 'D:', 'E:', 'F:', 'G:', 'Z:']
DriveInfoL = ['52.7GB | 119.7GB', '22.9GB | 80.0GB', '25.2GB | 50.0GB']
Drive_dict = {}
for i in range(3):
Drive_dict[DriveL[i]] = DriveInfoL[i]
Only the first 3 Entries are considered. If you want to use only NTFS drives, you have to find a way to figure out which entries in your DriveL
are NTFS drives.
Upvotes: 0
Reputation: 104
You can use a list comprehension to go through both lists at the same time
DriveL=['C:', 'D:', 'E:', 'F:', 'G:', 'Z:']
DriveInfoL=['52.7GB | 119.7GB', '22.9GB | 80.0GB', '25.2GB | 50.0GB']
Combined=[(x,y) for (x,y) in zip(DriveL,DriveInfoL)]
>>>Combined
>>>[('C:', '52.7GB | 119.7GB'),
('D:', '22.9GB | 80.0GB'),
('E:', '25.2GB | 50.0GB')]
Upvotes: 1