Python未正确拆分CRLF

2024-09-25 00:35:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个脚本,用python将非常简单的函数文档转换为XML。我使用的格式将转换为:

date_time_of(date) Returns the time part of the indicated date-time value, setting the date part to 0.

收件人:

^{pr2}$

到目前为止,它工作得很好(我上面发布的XML是从程序中生成的),但问题是它应该处理粘贴的几行文档,但它只适用于粘贴到应用程序中的第一行。我在Notepad++中检查了粘贴的文档,这些行的末尾确实有CRLF,那么我的问题是什么? 这是我的代码:

mainText = input("Enter your text to convert:\r\n")

try:
    for line in mainText.split('\r\n'):
        name = line.split("(")[0]
        arg = line.split("(")[1]
        arg = arg.split(")")[0]
        hlp = line.split(")",1)[1]
        print('<item name="%s">\r\n<arg>(%s)</arg>\r\n<help>%s</help>\r\n</item>\r\n' % (name,arg,hlp))
except:
    print("Error!")

你知道这是什么问题吗? 谢谢。在


Tags: ofthetoname文档datetime粘贴
3条回答

input()只读取一行。在

试试这个。输入空行以停止收集行。在

lines = []
while True:
    line = input('line: ')
    if line:
        lines.append(line)
    else:
        break
print(lines)

处理来自标准输入(控制台)的读取行的最佳方法是迭代系统标准对象。重写后,您的代码将如下所示:

from sys import stdin
try:
  for line in stdin:
    name = line.split("(")[0]
    arg = line.split("(")[1]
    arg = arg.split(")")[0]
    hlp = line.split(")",1)[1]
    print('<item name="%s">\r\n<arg>(%s)</arg>\r\n<help>%s</help>\r\n</item>\r\n' % (name,arg,hlp))
except:
    print("Error!")

也就是说,值得注意的是,在正则表达式的帮助下,解析代码可以大大简化。下面是一个例子:

^{pr2}$

我希望这有助于简化代码。在

帕特里克·莫里亚蒂

在我看来,你并没有特别提到游戏机,你主要关心的是一次把几条线连在一起接受治疗。只有一种方法可以重现您的问题:那就是,在空闲状态下执行程序,从一个文件中手动复制几行并粘贴到raw_input()

为了理解你的问题,我得出了以下事实:

  • 当数据从文件复制并粘贴到raw_input()时,新行\r\n被转换为\n,因此{}返回的字符串没有{}。因此,在这个字符串上不可能有split('\r\n')

  • 在记事本+窗口中粘贴包含孤立的\r\n字符的数据,并激活特殊字符的显示,它会在所有行尾出现crlf符号,甚至在只有\r\n的地方。因此,使用记事本+来验证换行符的性质会导致错误的结论

一。在

第一个事实是你问题的起因。我忽略了这个转换影响从文件复制并传递到raw_input()的数据的先前原因,这就是为什么我在stackoverflow上发布了一个问题:

Strange vanishing of CR in strings coming from a copy of a file's content passed to raw_input()

第二个原因是你的困惑和绝望。不可能。。。。在

一。在

那么,怎么解决你的问题呢?在

下面是一个重现这个问题的代码。注意其中修改的算法,替换了应用于每一行的重复拆分。在

ch = "date_time_of(date) Returns the time part.\r\n"+\
     "divmod(a, b) Returns quotient and remainder.\r\n"+\
     "enumerate(sequence[, start=0]) Returns an enumerate object.\r\n"+\
     "A\rB\nC"

with open('funcdoc.txt','wb') as f:
    f.write(ch)

print "Having just recorded the following string in a file named 'funcdoc.txt' :\n"+repr(ch)

print "open 'funcdoc.txt' to manually copy its content, and paste it on the following line"
mainText = raw_input("Enter your text to convert:\n")
print "OK, copy-paste of file 'funcdoc.txt' ' s content has been performed"


print "\nrepr(mainText)==",repr(mainText)

try:
    for line in mainText.split('\r\n'):  
        name,_,arghelp  = line.partition("(")
        arg,_,hlp = arghelp.partition(") ")
        print('<item name="%s">\n<arg>(%s)</arg>\n<help>%s</help>\n</item>\n' % (name,arg,hlp))
except:
    print("Error!")

一。在

以下是delnan提到的解决方案:«从源代码读取,而不是人工复制并粘贴它。» 它与您的split('\r\n')配合使用:

^{pr2}$

一。在

最后,这里是Python处理修改后的人类副本的解决方案:提供splitlines()函数,该函数将所有类型的换行符(\r或{}或{})作为拆分器。所以更换

for line in mainText.split('\r\n'):

通过

for line in mainText.splitlines():

相关问题 更多 >