我正在编写一个脚本,用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!")
你知道这是什么问题吗? 谢谢。在
input()
只读取一行。在试试这个。输入空行以停止收集行。在
处理来自标准输入(控制台)的读取行的最佳方法是迭代系统标准对象。重写后,您的代码将如下所示:
也就是说,值得注意的是,在正则表达式的帮助下,解析代码可以大大简化。下面是一个例子:
^{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()
第二个原因是你的困惑和绝望。不可能。。。。在
一。在
那么,怎么解决你的问题呢?在
下面是一个重现这个问题的代码。注意其中修改的算法,替换了应用于每一行的重复拆分。在
一。在
以下是delnan提到的解决方案:«从源代码读取,而不是人工复制并粘贴它。» 它与您的
^{pr2}$split('\r\n')
配合使用:一。在
最后,这里是Python处理修改后的人类副本的解决方案:提供}或{})作为拆分器。所以更换
splitlines()
函数,该函数将所有类型的换行符(\r
或{通过
相关问题 更多 >
编程相关推荐