Python remove rn from bytes

Asked 4 years, 1 month ago

Viewed 5k times

Given this string

b'141\r\n'

got by serial.readlines[], how can I extrapolate 141, or however the number in its position? Actually I can remove b' simply by using string[2:] but I cannot remove the remainder in this way. string.replace does not work I don't know why.

The number of figures in the number may vary. Is there a smart way to avoid this problem?

gre_gor

6,2289 gold badges42 silver badges48 bronze badges

asked Aug 29, 2018 at 13:44

6

Ok so theres two questions here. Is your object

b = b'141\r\n'

or

s = "b'141\\r\\n'"

If it's the former

b.decode[].strip[]

if it's the latter

import re
re.search[r'\d+', s].group[0]

answered Aug 29, 2018 at 13:55

FHTMitchellFHTMitchell

11.2k2 gold badges33 silver badges46 bronze badges

Remove \r\n from a string [or list of strings] in Python #

Use the str.rstrip[] method to remove \r\n from a string in Python, e.g. result = my_str.rstrip[]. The str.rstrip[] method returns a copy of the string with the trailing whitespace removed.

Copied!

my_str = "Hello world\r\n" # ✅ remove \r\n from string result = my_str.rstrip[] print[repr[result]] # 👉️ 'Hello world' # ------------------------------------- # ✅ remove \r\n from all strings in list with open['example.txt', 'r', encoding='utf-8'] as f: lines = f.readlines[] print[lines] # 👉️ ['First line\r\n', 'Second line\r\n', 'Third line\r\n'] result = [line.rstrip[] for line in lines] print[result] # 👉️ ['First line', 'Second line', 'Third line']

The \r\n character is used as a newline character in Windows.

The str.rstrip method returns a copy of the string with the trailing whitespace removed.

Copied!

my_str = "Hello world\r\n" # ✅ remove \r\n from string result = my_str.rstrip[] print[repr[result]] # 👉️ 'Hello world'

There is also a str.strip method, which returns a copy of the string with the leading and trailing whitespace removed.

Copied!

my_str = "\r\nHello world\r\n" result = my_str.strip[] print[repr[result]] # 👉️ 'Hello world'

You can also use the re.sub method to remove all \r\n characters in the string.

Copied!

import re my_str = "\r\nHello world\r\n" result = re.sub[r'\r\n', '', my_str] print[repr[result]] # 👉️ 'Hello world'

The re.sub method returns a new string that is obtained by replacing the occurrences of the pattern with the provided replacement.

The first argument we passed to the method is a regular expression that matches \r\n characters, and the second is the replacement string.

To replace \r\n from a list of strings:

  1. Use a list comprehension to iterate over the list.
  2. Call the str.rstrip[] method to remove trailing \r\n characters.
  3. The strings in the new list won't contain any \r\n characters.

Copied!

with open['example.txt', 'r', encoding='utf-8'] as f: lines = f.readlines[] print[lines] # 👉️ ['First line\r\n', 'Second line\r\n', 'Third line\r\n'] result = [line.rstrip[] for line in lines] print[result] # 👉️ ['First line', 'Second line', 'Third line']

We used a list comprehension to iterate over the list.

List comprehensions are used to perform some operation for every element, or select a subset of elements that meet a condition.

On each iteration, we call the str.rstrip[] method on the string to remove any trailing whitespace.

You could also call the str.strip[] method to remove all leading and trailing whitespace or the re.sub[] method to remove all \r\n characters from the string.

Copied!

import re # ✅ remove \r\n from all strings in list with open['example.txt', 'r', encoding='utf-8'] as f: lines = f.readlines[] print[lines] # 👉️ ['First line\r\n', 'Second line\r\n', 'Third line\r\n'] result = [re.sub[r'\r\n', '', line] for line in lines] print[result] # 👉️ ['First line', 'Second line', 'Third line']

The example above assumes that there is a file named example.txt located in the same directory as the Python script.

The file has the following contents:

Copied!

First line Second line Third line

How do I get rid of RN in Python?

Use the str. rstrip[] method to remove \r\n from a string in Python, e.g. result = my_str. rstrip[] .

What does \r do in Python?

In Python strings, the backslash "\" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return. Conversely, prefixing a special character with "\" turns it into an ordinary character.

How do you remove NR from a string in Python?

Method 1: Use the replace function to Remove a Newline Character From the String in Python..
Method 2: Use the strip[] Function to Remove a Newline Character From the String in Python..
Method 4: Use the re. sub[] Function to Remove a Newline Character From the String in Python..

Chủ Đề