【问题标题】:Convert a Python block of code into a single line将 Python 代码块转换为一行
【发布时间】:2022-04-26 07:59:36
【问题描述】:

下面是python块代码,

def compute_hcf(x, y):
   while(y):
       x, y = y, x % y
   return x

我想将其转换为如下所示的单行,

def compute_hcf(x, y):\n\t while(y): \n\t\t x, y = y, x % y \n\t return x

我有以下方法来解决这个问题,

def convert(fname):
    newLines = ''
    with open(fname, 'r') as f:
        for line in f:
            num_spaces = 0
            
            for letter in line:
                if letter != ' ':
                    break
                num_spaces += 1

            if num_spaces == 3:
                line = line.replace(' '*3, '\\t') #If number of Spaces is 3, replace with a single tab
            elif num_spaces == 4:
                line = line.replace(' '*4, '\\t') #If number of Spaces is 4, replace with a single tab
            elif num_spaces == 7:
                line = line.replace(' ' *7, '\\t\\t') #If number of Spaces is 7, replace with double tab
            elif num_spaces == 8:
                line = line.replace(' ' * 8, '\\t\\t') #If number of Spaces is 8, replace with double tab
            line = line + '\\n' #Adding new line at the end of the line
            newLines = newLines + line
    converted = ''.join(newLines)
    print(converted)
    
convert('fileWithInputContent.txt')

实际输出:

def compute_hcf(x, y):
\n\twhile(y):
\n\t\tx, y = y, x % y
\n\treturn x\n

预期输出:

def compute_hcf(x, y):\n\t while(y): \n\t\t x, y = y, x % y \n\t return x

【问题讨论】:

  • 请问为什么你要这样做?可能,你只需要result = repr(f.read())
  • 我需要它来维护它作为我的数据集的目标属性。谢谢它帮助
  • result = repr(f.read()) 仅处理 '\n' 。你能告诉我如何处理标签 - '\t'

标签: python python-3.x string logic converters


【解决方案1】:

当您在 python 中从文件中读取文本时,会自动在每行的末尾添加一个换行符。所以当我们打印出 python 读取的字符串时,它看起来像这样。

image showing that there is a newline character at the end of the string

这就是实际输出有换行符的原因。所以为了解决这个问题,我们只需要像这样删除换行符。

if '\n' in line:
     line = line.replace('\n', '')

添加了这个的代码如下所示。

def convert(fname):
newLines = ''
with open(fname, 'r') as f:
    for line in f:
        # print each string
        # print(repr(line))
        if '\n' in line:
            line = line.replace('\n', '')
        num_spaces = 0
        
        for letter in line:
            if letter != ' ':
                break
            num_spaces += 1

        if num_spaces == 3:
            line = line.replace(' '*3, '\\t') #If number of Spaces is 3, replace with a single tab
        elif num_spaces == 4:
            line = line.replace(' '*4, '\\t') #If number of Spaces is 4, replace with a single tab
        elif num_spaces == 7:
            line = line.replace(' ' *7, '\\t\\t') #If number of Spaces is 7, replace with double tab
        elif num_spaces == 8:
            line = line.replace(' ' * 8, '\\t\\t') #If number of Spaces is 8, replace with double tab
        line = line + '\\n' #Adding new line at the end of the line
        newLines = newLines + line
        
converted = ''.join(newLines)
print(converted)

它有效!

def compute_hcf(x, y):\n\twhile(y):\n\t\tx, y = y, x % y\n\treturn x\n

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-05
    • 2019-06-26
    • 2015-09-05
    • 2013-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多