英文:
Read file with strange separations in python
问题
我正在尝试自动化一个流程。但其中一个文件的分隔方式非常奇怪。
有人有解决方法吗?
非常感谢!
英文:
i'm trying to automate a process. But one of the files have a very strange separation.
The columns are separated by spaces, but some rows have more spaces then others
Any one have idea how solve this.
Thank a lot!
答案1
得分: 0
首先,我会确保这不是您用于查看文件的界面的工件,因为其中一些界面实际上可能只是显示制表符。您可以使用正则表达式在多个空格上拆分文件。
import re
for line in file:
fields = re.split("\s+", line)
英文:
First of all, I'd make sure that this is not an artefact of the interface you use for viewing the file, because some of them might actually just display tabs like this.
You can split the file using regular expressions on multiple spaces.
import re
for line in file:
fields = re.split("\s+", line)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论