在同一行打印; mantain直到被新​​字符串替换

时间:2016-07-08 09:37:57

标签: python

我想编写一个在控制台行上输出消息的函数。该消息应保留,直到我不再使用新消息调用相同的功能。此时,新消息应出现在替换它的旧消息的同一行上。

import sys
from time import sleep

def printSameLine(my_message):

    sys.stdout.write("Message: %s   \r" % (my_message))
    sys.stdout.flush()

    return

for i in range(1,10):

    printSameLine(str(i))
    sleep(4)

这似乎没有获得所需的结果

3 个答案:

答案 0 :(得分:1)

public class RecyclerViewActivity extends AppCompatActivity implements Callback<String> { private int count=0; private SwipeRefreshLayout swipeContainer; @InjectView(R.id.recycler_view) RecyclerView recyclerView; private StaggeredGridLayoutManager mStaggeredLayoutManager; private final RecyclerBinderAdapter<TitleSection, CardViewType> adapter = new RecyclerBinderAdapter<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_recycler_view); ButterKnife.inject(this); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); // Lookup the swipe container view swipeContainer = (SwipeRefreshLayout) findViewById(R.id.swipeContainer); // Setup refresh listener which triggers new data loading swipeContainer.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { // Your code to refresh the list here. // Make sure you call swipeContainer.setRefreshing(false) // once the network request has completed successfully. fetchTimelineAsync(count); count++; } }); mStaggeredLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL); recyclerView.setLayoutManager(mStaggeredLayoutManager); recyclerView.setItemAnimator(new DefaultItemAnimator()); recyclerView.setHasFixedSize(true); recyclerView.setAdapter(adapter); // Configure the refreshing colors swipeContainer.setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light, android.R.color.holo_orange_light, android.R.color.holo_red_light); fetchTimelineAsync(0); count++; Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://xomorod.com") .addConverterFactory(GsonConverterFactory.create()) .build(); SimpleService.StackOverflowAPI stackOverflowAPI = retrofit.create(SimpleService.StackOverflowAPI.class); Call<String> call = stackOverflowAPI.loadQuestions("android"); //asynchronous call call.enqueue(this); } public void fetchTimelineAsync(int page) { } @Override public void onResponse(Response<String> response, Retrofit retrofit) { setProgressBarIndeterminateVisibility(false); CardPic cardPic=new CardPic(this,new PicData("Title" +response.body(),response.body(),"https://pixabay.com/static/uploads/photo/2015/04/10/00/41/yellow-715540_960_720.jpg")); adapter.add(TitleSection.SECTION_3, cardPic); } @Override public void onFailure(Throwable t) { String ss=t.getMessage(); ss=ss+"sss"; } 不清除该行 - 它清除缓冲区(换句话说,打印行上的所有内容)。您可以做的是打印一长串sys.stdout.flush以清除当前行,但它也不会再回到换行符。尝试:

\b

否则,只需将sys.stdout.write('\b'*100) sys.stdout.write("Message: %s \r" % (my_message)) 放在写作开头即可获得好结果:

\r

但如果您的新消息比之前更短,那么这将不会很好:

sys.stdout.write("\rMessage: %s" % (my_message))

答案 1 :(得分:0)

如果可以使用print语句,您可以通过以下方式在python 3.x中获得所需的结果:

print("Message " + my_message + "\r", end='')

或在python 2中附加逗号:

print "Message " + my_message + "\r",

所以你基本上可以这样做:

import sys
from time import sleep

def printSameLine(my_message):
     print("Message: %s   \r" % (my_message), end='')
     return

for i in range(1,10):
    printSameLine(str(i))
    sleep(4)

答案 2 :(得分:0)

在使用更新的消息调用print之前,您可以使用以下代码清除控制台。但是,如果您在控制台中有其他行信息,则必须重新打印它们。

os.system('cls' if os.name=='nt' else 'clear')