Python - 将表情符号字符写入日志文件

时间:2017-11-19 03:50:06

标签: python logging encoding emoji

我在将。表情符号写入.log文件时遇到了困难。这是我的代码的相关snipet:

with open("testLog.log", "a") as myfile:
    print (message.content)                  #print to console - for debugging only
    print (message.content.encode('utf-8'))  #print to console - for debugging only
    myfile.write(message.content)

这是当message.content ='你好时输出到我的控制台的内容! “

hello there!                               
b'hello there! \xf0\x9f\x91\x8d'
Ignoring exception in on_message
Traceback (most recent call last):
    File "C:\path\to\file\file.py", line 29, in on_message
    myfile.write(message.content)
    File "C:\Python\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
    UnicodeEncodeError: 'charmap' codec can't encode character '\U0001f44d' in position 13: character maps to <undefined>

我环顾四周并尝试了一些解决方案,但无济于事。这个错误是由于我的日志文件是如何编码的吗?如果是这样,我如何更改编码以允许utf-8字符?

允许但不是更可取的解决方案是检测字符串中是否存在这些字符的方法,这样我就可以不将内容写入日志。

1 个答案:

答案 0 :(得分:0)

这应该可以解决您的问题。发件人:https://stackoverflow.com/a/43813727/6579239

<?php
$conn_error = "Could not connect";
// SQL connection credentials

//They are blanked out since it is connected to the server already

$mysql_host = "";
$mysql_user = "";
$mysql_pass = "";
$mysql_name = "";

$conn = new mysqli($mysql_host, $mysql_user, $mysql_pass,$mysql_name);

if ($db->connect_error) {
    die("Connection failed: " . $db->connect_error);
}
?>
    </div>
    </div>

    <body>
    <form id="form" method="post" action="">
        <input type="checkbox" name="price1" class="checkbox" <?=(isset($_POST['price'])?' checked':'')?>/> $0 - 5<br>
        <input type="checkbox" name="price2" class="checkbox" <?=(isset($_POST['price'])?' checked':'')?>/> $6 - 10<br>
        <input type="checkbox" name="price3" class="checkbox" <?=(isset($_POST['price'])?' checked':'')?>/> $11 - 20<br>
    </form>

    <?php
        if($_SERVER["REQUEST_METHOD"] == "POST") {
            if (isset($_POST["price1"])){
                $query = $conn->query("SELECT * FROM item WHERE price BETWEEN 0 AND 5");
            } elseif (isset($_POST["price2"])){
                $query = $conn->queryn("SELECT * FROM item WHERE price BETWEEN 6 AND 10");
            } elseif (isset($_POST["price3"])){
                $query = $conn->query("SELECT * FROM item WHERE price BETWEEN 11 AND 20");
            } else {
                $query = $conn->query("SELECT price * FROM item");
            }

            if($query->num_rows > 0){
                while($row = $query->fetch_assoc()){
                    ?>

                    <div class="list-item">
                        <h2><?php echo $row["name"]; ?></h2>
                        <h4>Price: <?php echo $row["price"]; ?></h4>
                    </div>
                <?php }
            }else{
                echo  'Product(s) not found';
            }
        }
    ?>
    </body>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.0/jquery.min.js"></script>
    <script type="text/javascript">
        $(function(){
            $('.checkbox').on('change',function(){
                $('#form').submit();
            });
        });
    </script>