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
  • Please show the code you're having trouble with - Minimal Reproducible Example Also, don't post hyperlinks to images – Adon Bilivit Commented Feb 11 at 10:25
  • Ok, now you just need to tell us what is the output you are getting and how is different from the output you expect. – Cincinnatus Commented Feb 11 at 10:53
  • About your code, I notice Lines is not defined; furthemore, are you sure the indexes in words2 should not begin at 0 ? In addition, if you want to use regex, it would be simpler to look for "words" (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:00
  • _It is not possible to combine these values._Just looking at that, you absolutely can. If that is your problem, this might fix it. The program takes the numbers as strings and puts them into one string, which it casts to an int. That it works, can be tested by adding 1. str1 = "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
Add a comment  | 

1 Answer 1

Reset to default 0

If 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