用Python读取具有奇怪分隔符的文件

huangapple go评论57阅读模式
英文:

Read file with strange separations in python

问题

我正在尝试自动化一个流程。但其中一个文件的分隔方式非常奇怪。

列之间是用空格分隔的,但有些行之间的空格数量不同

有人有解决方法吗?

非常感谢! 用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! 用Python读取具有奇怪分隔符的文件

答案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)

huangapple
  • 本文由 发表于 2023年2月17日 23:41:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75486382.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定