繁体   English   中英

将带有“ \\ LF”的行添加到一行中,直到找到“ \\ CR \\ LF”?

[英]append lines with “\LF” into one line, until finds “\CR\LF”?

我正在寻找这种情况的解决方案,它是文件清除。 我有一个包含多行的文件“ * .csv”,所有行的末尾都有“ \\ CR \\ LF”,有时该文件带有虚线,因此最后只有“ \\ LF”,而缺少“ \\ CR”。 我需要将所有仅带有“ \\ LF”的行放到一行中,没有任何空格,最后也要有“ \\ CR \\ LF”。

例如,

这是文件内容的Python表示形式:

file_content = '''\
"A",B,"C","D"\r\n\
"E",F,"G","H"\r\n\
"I",J\n\
       \n\
             ,"K",    \n\
\n\
"L"\r\n\
"O",P,"Q","R"\r\n\
"S",T,"U","V"\r\n\
'''

两种可能的解决方案是:

import re

file_content = '''\
"A",B,"C","D"\r\n\
"E",F,"G","H"\r\n\
"I",J\n\
       \n\
             ,"K",    \n\
\n\
"L"\r\n\
"O",P,"Q","R"\r\n\
"S",T,"U","V"\r\n\
'''

print "Original:\n", file_content

replace1 = re.sub("(?<!\r) *\n *", '', file_content)
print "Replace1:\n", replace1

replace2 = re.sub("([^\r])( *\n *)+", '\\1', file_content)
print "Replace2:\n", replace2

该Python 2脚本的输出为:

Original:
"A",B,"C","D"
"E",F,"G","H"
"I",J

             ,"K",    

"L"
"O",P,"Q","R"
"S",T,"U","V"

Replace1:
"A",B,"C","D"
"E",F,"G","H"
"I",J,"K","L"
"O",P,"Q","R"
"S",T,"U","V"

Replace2:
"A",B,"C","D"
"E",F,"G","H"
"I",J,"K","L"
"O",P,"Q","R"
"S",T,"U","V"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM