V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
qile11
V2EX  ›  问与答

Python queue 不支持特殊字符?

  •  
  •   qile11 · Aug 2, 2018 · 1895 views
    This topic created in 2827 days ago, the information mentioned may be changed or developed.

    如题,刚刚测试了下 回车符\x0D 无法入队列

    import queue, time
    q = queue.Queue()
    
    for i in range(5):
        q.put(str(i)+"\x0B"+"|\x0D\x1c\x0D")
    while not q.empty():
        print (q.get())
    
    q = queue.Queue()
    
    for i in range(5):
        q.put(str(i)+"\x0B"+"|\r\x1c\r")
    
    while not q.empty():
        print (q.get())
    

    这样可以 \r 换为\n

    import queue, time
    q = queue.Queue()
    
    for i in range(5):
        q.put(str(i)+"\x0B"+"|\n\x1c\n")
    
    while not q.empty():
        print (q.get())
    

    打印出来 没有内容,忘指点下代码那里错误了? python 3.6 win10 64 系统

    6 replies    2018-08-04 22:45:07 +08:00
    msg7086
        1
    msg7086  
       Aug 3, 2018
    你贴的整段代码里也没有检查字符串是否进入队列的代码呀,你是怎么看出「无法入队列」的?

    回车符本来就是看不见的,什么叫打印出来没有内容?我这里能看到正常打印出了所有的内容。
    qile11
        2
    qile11  
    OP
       Aug 3, 2018
    @msg7086 我打印出来没有“|”这个内容,我从队列获取后写入文本文件也没有内容!
    msg7086
        3
    msg7086  
       Aug 3, 2018
    x0B 是控制字符吧。
    你有可以复现写入文本文件的源代码么,能不能贴个 gist ?
    我跑了你主题里的代码,能看见竖线。
    qile11
        4
    qile11  
    OP
       Aug 4, 2018
    我重新测试发现确实好使,比较乌龙了!
    是不是和我内存有关系
    或者因为多线程导致问题错误!
    我前天运行发现嘿嘿的没有内容才发帖的,现在居然好使!
    qile11
        5
    qile11  
    OP
       Aug 4, 2018
    刚刚运行又不好使了
    qile11
        6
    qile11  
    OP
       Aug 4, 2018
    https://s1.ax1x.com/2018/08/04/PDkwef.png
    运行截图图片地址
    ````
    for i in range(5):
    q.put(str(i)+"\x0B"+"|\x0D\x1c\x0D")
    l=[]
    while not q.empty():
    print(q.get())
    ````
    上面为运行代码
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3778 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 53ms · UTC 10:31 · PVG 18:31 · LAX 03:31 · JFK 06:31
    ♥ Do have faith in what you're doing.