如何使用python缩进文件中的所有列

如何使用python缩进文件中的所有列,python,parsing,indentation,Python,Parsing,Indentation,我希望使用python脚本以特定顺序对齐文件每列中的所有字符串。我已经用一个示例场景描述了问题和可能的结果 #sample.txt start() "hello" appended() "fly" instantiated() "destination" do() "increment" logging_sampler() "dummy string" 输出场景 #sample.txt(indented) start() "hello" appended()

我希望使用python脚本以特定顺序对齐文件每列中的所有字符串。我已经用一个示例场景描述了问题和可能的结果

#sample.txt

start() "hello"
appended() "fly"
instantiated() "destination"
do() "increment"
logging_sampler() "dummy string"
输出场景

#sample.txt(indented)

start()           "hello"
appended()        "fly"
instantiated()    "destination"
do()              "increment"
logging_sampler() "dummy string"

那么,是否有任何python库可以处理文件并提供上述缩进?
如果我有一个包含两列以上的文件,并且我仍然可以以相同的方式缩进所有列,那么有没有一种通用的解决方案?

那么有没有任何python库可以处理一个文件并提供上述缩进

这可能吗

您需要知道如何
解析行
s,然后
以格式化方式显示

在您的特定情况下,
解析
非常简单,因为您只需要根据第一个出现的空格拆分字符串。这可以很容易地使用。有时,您甚至可能需要使用一些奇特的解析逻辑

格式设置
更简单,如果您知道

演示

>>> for e in st.splitlines():
    left,_,right = e.partition(' ')
    print "{:<20}{:<20}".format(left, right)


start()             "hello"             
appended()          "fly"               
instantiated()      "destination"       
do()                "increment"         
logging_sampler()   "dummy string"  
圣斯普林斯()中的e的
>:
左,右=e.分区(“”)

打印“{:那么是否有任何python库可以处理文件并提供上述缩进?

这可能吗?

您需要知道如何
解析行
s,然后
以格式化方式显示

在您的特定情况下,
解析
是直接的,因为您只需要根据第一个出现的空格拆分字符串。这可以很容易地使用来完成。有时您甚至可能需要一些您可能需要使用的外来解析逻辑

格式设置
更简单,如果您知道

演示

>>> for e in st.splitlines():
    left,_,right = e.partition(' ')
    print "{:<20}{:<20}".format(left, right)


start()             "hello"             
appended()          "fly"               
instantiated()      "destination"       
do()                "increment"         
logging_sampler()   "dummy string"  
圣斯普林斯()中的e的
>:
左,右=e.分区(“”)

打印“{:您可以在打印中使用制表符(“\t”),但我不清楚您是如何打印sample.txt的

print string1+"\t"+string2

有关更多详细信息,请参见

您可以在打印中使用制表符(“\t”),但我不清楚您是如何打印sample.txt的

print string1+"\t"+string2
有关更多详细信息,请参见改编自此处的函数获取字符串列表列表,并返回列表格式的行

def table(lines, delim='\t'):
    lens = [len(max(col, key=len)) for col in zip(*lines)]
    fmt = delim.join('{:' + str(x) + '}' for x in lens)
    return [fmt.format(*line) for line in lines]
其余的都是无关紧要的:

import re
with open(__file__) as fp:
    lines = [re.split(r' ', s.strip(), maxsplit=1) for s in fp]
print '\n'.join(table(lines))

改编自下面的函数,获取字符串列表列表,并返回列表格式的行

def table(lines, delim='\t'):
    lens = [len(max(col, key=len)) for col in zip(*lines)]
    fmt = delim.join('{:' + str(x) + '}' for x in lens)
    return [fmt.format(*line) for line in lines]
其余的都是无关紧要的:

import re
with open(__file__) as fp:
    lines = [re.split(r' ', s.strip(), maxsplit=1) for s in fp]
print '\n'.join(table(lines))

上述代码适用于给定的场景。但是,如果我有一个包含两列以上的文件,并且我仍然需要以相同的方式缩进所有列,那么有没有通用的解决方案?@karthik:不幸的是,没有专门用于此目的的心灵库。@karthik:您可以遍历左侧的项目,并找出其中的哪一项em通过使用最长。然后,您可以通过使用找到的最大长度的值并向其添加一些值(例如1)来应用所需的填充。上述代码适用于给定的场景。但是,如果我有一个包含2列以上的文件,并且我仍然需要以相同的方式缩进所有列,是否有任何通用解决方案?@karthik:不幸的是,没有用于此目的的通灵库。@karthik:您可以使用遍历左侧项目,找出其中最长的项目。然后,您可以使用找到的最大长度的值并向其添加一些值,例如1,来应用所需的填充。