Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在单行文本文件中的数字后添加新行_Python_Python 3.x - Fatal编程技术网

Python 在单行文本文件中的数字后添加新行

Python 在单行文本文件中的数字后添加新行,python,python-3.x,Python,Python 3.x,我在一个文本文件中有一个巨大的HPBasic代码字符串,例如: 158!159 160号出口!161 Prntr_可用:!如果打印机不是162,则不允许进入测试菜单!可用的;如果条件$(15,2)[6,6]“*”则结果仅转到打印机163!打印机不可用164秒错误=1 165提示用户(“错误:打印机不可用;无法执行测试”) 这些连续的数字是代码中的新行。我如何在这些数字之前反复打印一个换行符以使其可读?目前我有: mystring = ('EnormousString') myString.rep

我在一个文本文件中有一个巨大的HPBasic代码字符串,例如:

158!159 160号出口!161 Prntr_可用:!如果打印机不是162,则不允许进入测试菜单!可用的;如果条件$(15,2)[6,6]“*”则结果仅转到打印机163!打印机不可用164秒错误=1 165提示用户(“错误:打印机不可用;无法执行测试”)

这些连续的数字是代码中的新行。我如何在这些数字之前反复打印一个换行符以使其可读?目前我有:

mystring = ('EnormousString')
myString.replace('1', '1\n')

这类作品。有没有办法将
+=1
添加到这个列表中?不确定要带它去哪里。

您可以这样做:

output = []
curline = 0
for token in s.split(' '):
  try:
    line = int(token)
    if line > curline:
      curline = line
      output.append('\n')
  except:
    pass
  output.append(token)

output_str = ' '.join(output).lstrip() # lstrip removes the leading \n
这并不是假设行号都比最后一行大一(但可以加上),因为我相信BASIC只要求它比前一行大。正如其他人所提到的,如果行中有较大的数字(被空格包围),则此功能可能会中断。

此def将完成此功能(如下所示)。它要求所有连续的行号按顺序出现,并且我要求每个行号两侧都有空格,以减少由于(例如)第3行分隔符前面的文本中出现数字3而丢失信息的可能性。为了防止行拆分由于某种原因出现在第3行分隔符之后的“3”,我使用了maxslit=1(即
str.split([sep[,maxslit]])
),因此它只使用了“3”的第一个实例:


将其添加到文件中应该很简单。

这假设文本的第一部分始终是行号,如果输入有效,则应该是行号。它还假设行本身永远不会包含两个空格之间的下一行号;这不是一个很好的假设,但是如果不集成HPBasic解析器,我认为没有多少方法可以解决这个问题

code = """158 ! 159 SUBEXIT 160 ! 161 Prntr_available: ! Cannot allow entry to Test Menu if printer is not 162 ! available; results only go to printer 163 IF Conditions$(15,2)[6,6]<>"*" THEN ! Printer is not available 164 Cond_error=1 165 Prompt_user("ERROR: Printer not available; cannot perform tests.")"""
line_number = int(code[:code.index(" ")])

lines = []
string_index = 0

while True:
    line_number += 1

    try:
        next_index = code.index(" " + str(line_number) + " ", string_index)
    except ValueError:
        lines.append(code[string_index:].strip())
        break

    lines.append(code[string_index:next_index].strip())
    string_index = next_index

print "\n".join(lines)
# 158 ! 
# 159 SUBEXIT 
# 160 ! 
# 161 Prntr_available: ! Cannot allow entry to Test Menu if printer is not 
# 162 ! available; results only go to printer 
# 163 IF Conditions$(15,2)[6,6]<>"*" THEN ! Printer is not available 
# 164 Cond_error=1
# 165 Prompt_user("ERROR: Printer not available; cannot perform tests.")
code=”““158!159子出口160!161 Prntr\u可用:!如果打印机不可用,则无法允许进入测试菜单162!可用;如果条件$(15,2)[6,6]“*”则结果仅转到打印机163!打印机不可用164秒错误=1 165提示用户(“错误:打印机不可用;无法执行测试”)
行号=int(代码[:code.index(“”])
行=[]
字符串索引=0
尽管如此:
行号+=1
尝试:
下一个索引=代码索引(“+str(行号)+”,字符串索引)
除值错误外:
lines.append(代码[string_index:].strip())
打破
line.append(代码[字符串索引:下一个索引].strip())
字符串索引=下一个索引
打印“\n”。连接(行)
# 158 ! 
#159次出口
# 160 ! 
#161 Prntr_可用:!如果打印机未启动,则不允许进入测试菜单
# 162 ! 可用的;结果只进入打印机
#163如果条件$(15,2)[6,6]“*”那么!打印机不可用
#164秒误差=1
#165提示用户(“错误:打印机不可用;无法执行测试。”)

如果找到3位数字,用regex替换怎么样

import re
mystring = '158 ! 159 SUBEXIT 160 ! 161 Prntr_available: ! Cannot allow entry to Test Menu if printer is not 162 ! available; results only go to printer 163 IF Conditions$(15,2)[6,6]<>"*" THEN ! Printer is not available 164 Cond_error=1 165 Prompt_user("ERROR: Printer not available; cannot perform tests.")'
print((re.sub(r"(\d{3})",r"\n\1", mystring)))
重新导入
mystring='158!159 160号出口!161 Prntr_可用:!如果打印机不是162,则不允许进入测试菜单!可用的;如果条件$(15,2)[6,6]“*”则结果仅转到打印机163!打印机不可用164秒错误=1 165提示用户(“错误:打印机不可用;无法执行测试”。)
print((re.sub(r“(\d{3})”,r“\n\1”,mystring)))
这将产生以下输出:

158 ! 
159 SUBEXIT 
160 ! 
161 Prntr_available: ! Cannot allow entry to Test Menu if printer is not 
162 ! available; results only go to printer 
163 IF Conditions$(15,2)[6,6]<>"*" THEN ! Printer is not available 
164 Cond_error=1 
165 Prompt_user("ERROR: Printer not available; cannot perform tests.")
158!
159次出口
160 ! 
161 Prntr_可用:!如果打印机未启动,则不允许进入测试菜单
162 ! 可用的;结果只进入打印机
163如果条件$(15,2)[6,6]“*”那么!打印机不可用
164秒误差=1
165提示用户(“错误:打印机不可用;无法执行测试。”)
158 ! 
159 SUBEXIT 
160 ! 
161 Prntr_available: ! Cannot allow entry to Test Menu if printer is not 
162 ! available; results only go to printer 
163 IF Conditions$(15,2)[6,6]<>"*" THEN ! Printer is not available 
164 Cond_error=1 
165 Prompt_user("ERROR: Printer not available; cannot perform tests.")