admin管理员组文章数量:1302328
I have a test.txt.
The content of test.txt is shown below:
| 55 | 77 | 02 | 30 | A0 | 10 | D0 | 11 | 78 | 70 | 99 | 19 | 00 | A0 | 0B | AA |
| 55 | 77 | 02 | 30 | A0 | 10 | D0 | 11 | 78 | 70 | 99 | 19 | 00 | A0 | 0B | AA |
| 55 | 77 | 02 | 30 | A0 | 10 | D0 | 11 | 78 | 70 | 99 | 19 | 00 | A0 | 0B | AA |
I want to convert string to hex and save it to test.csv.
The expected results are shown below:
30027755
11D010A0
19997078
AA0BA000
30027755
11D010A0
19997078
AA0BA000
30027755
11D010A0
19997078
AA0BA000
I have tried the following code, but the output is not what I want.
import re
import numpy as np
import matplotlib.pyplot as plt
def split(delimiters, string, maxsplit=0):
regexPattern = '|'.join(map(re.escape, delimiters))
return re.split(regexPattern, string, maxsplit)
f = open('test\\test.txt')
n = 0
while n < len(Lines):
delimiters = " "
words = split(delimiters, Lines[n])
print(n,words,len(words))
delimiters = " "
words2 = split(delimiters, Lines[n])
print(words2,"LEN=",len(words2))
delimiters = "|", "\n"
words2 = split(delimiters, Lines[n])
print(words2[4]+words2[3]+words2[2]+words2[1])
n = n + 1
It is not possible to combine these values.
30 02 77 55
I have a test.txt.
The content of test.txt is shown below:
| 55 | 77 | 02 | 30 | A0 | 10 | D0 | 11 | 78 | 70 | 99 | 19 | 00 | A0 | 0B | AA |
| 55 | 77 | 02 | 30 | A0 | 10 | D0 | 11 | 78 | 70 | 99 | 19 | 00 | A0 | 0B | AA |
| 55 | 77 | 02 | 30 | A0 | 10 | D0 | 11 | 78 | 70 | 99 | 19 | 00 | A0 | 0B | AA |
I want to convert string to hex and save it to test.csv.
The expected results are shown below:
30027755
11D010A0
19997078
AA0BA000
30027755
11D010A0
19997078
AA0BA000
30027755
11D010A0
19997078
AA0BA000
I have tried the following code, but the output is not what I want.
import re
import numpy as np
import matplotlib.pyplot as plt
def split(delimiters, string, maxsplit=0):
regexPattern = '|'.join(map(re.escape, delimiters))
return re.split(regexPattern, string, maxsplit)
f = open('test\\test.txt')
n = 0
while n < len(Lines):
delimiters = " "
words = split(delimiters, Lines[n])
print(n,words,len(words))
delimiters = " "
words2 = split(delimiters, Lines[n])
print(words2,"LEN=",len(words2))
delimiters = "|", "\n"
words2 = split(delimiters, Lines[n])
print(words2[4]+words2[3]+words2[2]+words2[1])
n = n + 1
It is not possible to combine these values.
30 02 77 55
Share
edited Feb 11 at 11:33
RiRi
asked Feb 11 at 10:20
RiRiRiRi
11 bronze badge
4
|
1 Answer
Reset to default 0If your only question is how to join the words, you can find the solution here.
For strings, + works differently than for numbers.
str1="abc"
str2="def"
str3=str1+str2
print(str3)
will result in abcdef
.
However, this takes some time to write, so there are easier methods, which you can find at the linked post. See also the comments below the answer for clarification
本文标签: Convert file string to Hex in python and save as csvStack Overflow
版权声明:本文标题:Convert file string to Hex in python and save as .csv - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741665320a2391276.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
re.findall(r"(\w+)", line)
) on each line (or on the whole file actually). Then you can get slices of 4 elements of the obtained list, then reverse each slice and join its content. – Swifty Commented Feb 11 at 11:00str1 = "30" str2 = "02" str3 = "77" str4 = "55" finalstr = str1 + str2 + str3 + str4 print(finalstr) number = int(finalstr) print(number + 1)
– Fff Commented Feb 11 at 11:56