---
slug: "remove-4bytes-char-on-python"
title: "Python で文字列中の4バイト文字を消す"
description: "macOS の Finder で開いているファイルやフォルダの絶対パスを、Service (Quick Action) でクリップボードにコピーする設定方法。"
url: "https://www.ytyng.com/blog/remove-4bytes-char-on-python"
publish_date: "2023-03-06T07:01:43Z"
created: "2023-03-06T07:01:43Z"
updated: "2026-05-11T13:21:31.815Z"
categories: ["Python"]
keywords: ""
featured_image_url: "https://media.ytyng.com/resize/20250708/fbc7a05b18e44c2bb9ed93439cfd88c4.png.webp?width=768"
has_video: true
has_music: true
video_urls: ["https://media.ytyng.net/ytyng-blog/273/featured-video-1.mp4", "https://media.ytyng.net/ytyng-blog/273/featured-video-2.mp4", "https://media.ytyng.net/ytyng-blog/273/featured-video-3.mp4"]
music_urls: ["https://media.ytyng.net/ytyng-blog/273/featured-music-273-3.mp3", "https://media.ytyng.net/ytyng-blog/273/featured-music-273-4.mp3"]
lang: "ja"
---

# Python で文字列中の4バイト文字を消す

```python

def remove_4bytes_char(text):
    """
    文字列から4バイト文字を消す
    """
    # 文字列を bytearray に変換
    byte_string = bytearray(text.encode('utf-8'))

    # バイト列から4バイトのUTF-8文字を除去する
    while b'\xf0' in byte_string:
        index = byte_string.index(b'\xf0')
        if index + 3 < len(byte_string):
            for _i in range(4):
                byte_string.pop(index)

    # bytearrayを文字列に変換
    return byte_string.decode('utf-8')
```
