简体   繁体   中英

Python writing to terminal for no apparent reason?

I have a fairly straightforward python (3.5.2) script, which reads from a file, and if the line meets certain criteria, then it copies the line into another one. The original file looks like this (this is one line, many more follow, in the same format but different numbers):

2.9133 1 1 157.6578 170.4160 0.7081 3044.911 351.998 152.778 -1451.315 162.109

And my script goes like:

 inp = open("original.dat", "r")

 out = open("new.log", "w")

 for line in inp:

     if float(line.split()[5]) < 5.0:

        out.write(line)

The program finishes the task well, I get the desired output. However, while running from the python terminal window, it spams my terminal with integers around 80 (so like 70-90) seemingly in a random fashion.

I have no idea what might cause this undesired output, but it messes my workspace up hard. I hope some of you may have an idea to fix it.

This is a normal behavior when you do .write on a file like that. It's the number of bytes written to the file.

>>> a = open("new.txt", 'w')
>>> a.write("hello")
5

There's probably a more elegant solution, but you could suppress this by setting a variable to the output of out.write

foo = out.write(line)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM