如何使我的文字没有额外的行 [英] How do I make my text has no extra line

查看:126
本文介绍了如何使我的文字没有额外的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个程序。基本上我得到7个坐标,然后我发回了1个坐标。 (无法真正解释)

So I have this program. Basically I get 7 coordinates and I sent back 1 coordinate. (Cant really explain)

from sys import stdin ,stdout

test=int(stdin.readline().strip("\n")) #It got stripped the new line

for z in range(test):
    c=stdin.readline().strip("\n").split(" ")# Here also!
    x=[0,0,0,0,0,0,0,0]
    y=[0,0,0,0,0,0,0,0]
    letter="abcdefgh"
    for k in range(7):
        x[letter.index(c[k][0])]=1
        y[int(c[k][1])-1]=1
    stdout.write(letter[x.index(0)]+str(y.index(0)+1)+"\n") #This is the main bits

这就是事情。

此程序响应被捕获通过另一个程序并写入文件。当我使用 stdout.write(+\ n) print 时,我的程序会输出类似

This program respond is caught by another program and written in a file. When I use stdout.write(+"\n") or print, my program outputs something like

h4

g2

h4
...

但是当我只使用普通的 stdout.write()

But when I just use plain stdout.write()

h4g2h4...

如何使我的程序输出如下:

How do I make my program outputs something like this:

h4
g2
h5
...

记录我的程序的程序使用:

The program who records my program uses this:

result=open("txt.txt","w")
process=subprocess.Popen([sys.executable,"work.py"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
res=process.communicate(a)
result.write(res[0])

就我而言, print 增加1行,而不是2行。这就是我迷路的地方。

As far as I concern, print adds 1 line, not 2 lines. That's where I got lost.

我的输入确实有新的一行,但它被剥离了,你可以在第2行看到。

My input does have new line, but it got stripped, as you can see in line 2.

推荐答案

因此,由于您正在写一个文件,我可以根据我对python的了解来回答这个问题。如果我错了,请纠正我,但这是我的答案:

So since you are writing to a file I can sort of answer the question based upon my knowledge of python. Correct me if I am wrong, but here is my answer:

stdout.writelines([letters[x.index(0)]+str(y.index(0)+1)])

这可能效果不太好当你的x值发生变化等等。

This could not work very well as your x values change and so forth.

你可以像这样使用.format:

You could use .format like so :

stdout.write('{} \n'.format(letters[x.index(0)]+str(y.index(0)+1)))

但是如果你无法做到这一点,那么你可能就是如何格式化stdout.write()...

However if you can't make that work, then it may be how you are formatting stdout.write()...

Colin

这篇关于如何使我的文字没有额外的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆