我正在尝试从索引在位置[7]处的BLF日志文件中提取可变长度的十六进制值。我可以在列表中成功提取可变长度的“十六进制”值。 问题是从每个提取的列表中删除十六进制值之间的逗号。
下面是我要从中提取可变长度十六进制值的BLF文件:
['Timestamp:', '1546626931.138813', 'ID:', '0764', 'S', 'DLC:', '8', '00', '00', '00', '00', '00', '00', '00', '00', 'Channel:', '0']
['Timestamp:', '1546626931.138954', 'ID:', '0365', 'S', 'DLC:', '8', '00', '00', '00', '80', 'db', '80', 'a2', '7f', 'Channel:', '1']
['Timestamp:', '1546626931.139053', 'ID:', '0765', 'S', 'DLC:', '6', 'ae', '05', '00', '00', '05', '00', 'Channel:', '1']
['Timestamp:', '1546626931.139697', 'ID:', '022a', 'S', 'DLC:', '4', '40', 'c0', '50', '6c', 'Channel:', '1']
.
.
.
.
我在文件中得到的输出如下:
['00', '00', '00', '00', '00', '00', '00', '00']
['00', '00', '00', '80', 'db', '80', 'a2', '7f']
['ae', '05', '00', '00', '05', '00']
['40', 'c0', '50', '6c']
但是我想要的是如下内容:首先从原始列表amd中删除逗号,然后将下面显示的十六进制值转换为十进制:
['0000000000000000']
['00000080db80a27f']
['ae0500000500']
['40c0506c']
我的代码如下:
import can
import csv
import datetime
# import timestamp as timestamp
filename = open('C:\\Users\\xyz\\Downloads\\BLF File\\hex_Decimal.csv', "w")
log1 = can.BLFReader('C:\\Users\\xyz\\Downloads\\BLF File\\test.blf')
#Extracting Hexadecimal and convert into decimal
for time in log1:
time = str(time).split()
data=str(time[7:(7 + int(time[6]))])
"".join(data)
print(data)
我无法从列表中删除逗号并将十六进制数列表转换为列表内的十进制值。任何帮助表示赞赏。谢谢!
答案 0 :(得分:2)
您可以使用int(<string with hex number>, 16)
来转换十进制值:
data = [
['00', '00', '00', '00', '00', '00', '00', '00'],
['00', '00', '00', '80', 'db', '80', 'a2', '7f'],
['ae', '05', '00', '00', '05', '00'],
['40', 'c0', '50', '6c'],
]
for row in data:
s = ''.join(row)
print('{: <{}}: {}'.format(s, 16, int(s, 16)))
打印:
0000000000000000: 0
00000080db80a27f: 553438454399
ae0500000500 : 191336498070784
40c0506c : 1086345324
答案 1 :(得分:1)
如果从BLF文件中提取的time
消息的类型只是一个字符串列表,那么您已经完成了:
...
#Extracting Hexadecimal and convert into decimal
for time in log1:
data = int(''.join(time[7:7+int(time[6])]), 16)
print([data])
您应该得到:
[0]
[553438454399]
[191336498070784]
[1086345324]