英文:
How to take out spaces when reading a text file
问题
with open("zeaaa.txt", "rb") as file:
byte = file.read(6)
byte = byte.replace(b" ", b"") # Replace spaces with nothing
print(byte.decode())
while byte:
byte = file.read(6)
byte = byte.replace(b" ", b"") # Replace spaces with nothing
print(byte.decode())
英文:
I am trying to write code that will transform a 160x160 table (where each column is a value of 5 digits and a dot, ex: "1.02456") into a single column. For example, the code would transform:
1 2 3 4 5 ->
1
2
3
4
5
However, the code I have currently prints the columns with the spaces included. Here is the current code:
with open("zeaaa.txt", "rb") as file:
byte = file.read(6)
byte.replace(" ", "")
print(byte.decode)
while byte:
byte = file.read(6)
byte.replace(" ", "")
print(byte.decode())
How can I modify this code to remove the spaces and print the single column as described above?
答案1
得分: 1
查看Python文档:replace
返回 一个修改后的副本,不执行原地修改。由于您没有分配此结果,它将丢失。
英文:
See Python documentation: replace
returns a modified copy, it performs no in-place modification. Since you don't assign this result, it is lost.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论