te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>python - How do I erase the last word printed onto the terminal? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - How do I erase the last word printed onto the terminal? - Stack Overflow

programmeradmin3浏览0评论

I would like to be able to erase the last word printed onto the terminal. The last word should be the last string of text following a space. The following code simulates what I am trying to achieve. I want a function remove_last_word() that will erase str.

import random

words = ['apple', 'banana', 'cherry', 'dog', 'elephant', 'flower', 'guitar', 'house', 'island', 'jungle']
str = 'stop.\n'

for i in range(0, random.randint(0, 100)):
    print(random.choice(words), end=' ')
print(str, end='')

remove_last_word()

Simply printing a carriage return won't work because there might be a newline at the end of the word. Moving the cursor up a line and printing a carriage return won't work because that will overwrite the previous words printed onto the terminal. I need a way to access the contents of the terminal so that I can find the start of the the last word.

I would like to be able to erase the last word printed onto the terminal. The last word should be the last string of text following a space. The following code simulates what I am trying to achieve. I want a function remove_last_word() that will erase str.

import random

words = ['apple', 'banana', 'cherry', 'dog', 'elephant', 'flower', 'guitar', 'house', 'island', 'jungle']
str = 'stop.\n'

for i in range(0, random.randint(0, 100)):
    print(random.choice(words), end=' ')
print(str, end='')

remove_last_word()

Simply printing a carriage return won't work because there might be a newline at the end of the word. Moving the cursor up a line and printing a carriage return won't work because that will overwrite the previous words printed onto the terminal. I need a way to access the contents of the terminal so that I can find the start of the the last word.

Share Improve this question asked Feb 17 at 23:31 ERROR 404ERROR 404 111 bronze badge New contributor ERROR 404 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 3
  • You need to know how long the last word is. Print that many backspaces followed by that many spaces. You have to do this BEFORE you print the newline. – Barmar Commented Feb 17 at 23:35
  • 2 Don't use str as a variable name, it's the name of a built-in type. – Barmar Commented Feb 17 at 23:37
  • 4 If you're trying to create an application that manipulates the text on the screen, you should look into using the ncurses library. – Barmar Commented Feb 17 at 23:39
Add a comment  | 

1 Answer 1

Reset to default 0

I wasn't able to do this without curses, so here's a solution using curses:

import curses
import random

words = ['apple', 'banana', 'cherry', 'dog', 'elephant',
         'flower', 'guitar', 'house', 'island', 'jungle']

def remove_last_word(stdscr):
    # Clear the screen                                                                                                                                                                        
    stdscr.clear()

    # Randomized words list using the vocabulary provided                                                                                                                                                                      
    picked_words = [random.choice(words) for _ in range(random.randint(1, 100))]

    # Print selected words, separated by spaces                                                                                                                                               
    stdscr.addstr(' '.join(picked_words) + ' stop.\n')
    stdscr.refresh()

    # So we can observe the delete: wait for user input to remove the last word                                                                                                               
    stdscr.addstr("Press Enter to remove the last word...")
    stdscr.refresh()
    stdscr.getch()  # Wait for user to press a key                                                                                                                                            

    # Remove the last printed word using curses                                                                                                                                               
    #-----------------------------------------------------                                                                                                                                    
    # 1.) Clear the screen                                                                                                                                                                    
    stdscr.clear()
    if picked_words:
        # 2.) Remove the last word                                                                                                                                                            
        picked_words.pop()
        # 3.) print it out again with one less word                                                                                                                                               
        stdscr.addstr(' '.join(picked_words) + ' stop.\n')

    stdscr.refresh()
    stdscr.getch()  # Wait for another key press to exit                                                                                                                                      

def main():
    # Clearly, we are using curses to handle our console updates                                                                                                                              
    curses.wrapper(remove_last_word)

if __name__ == "__main__":
    main()
发布评论

评论列表(0)

  1. 暂无评论