python - 为什么输出不符合我的代码条件?
问题描述
当我的限定符不均匀时,我在这里有这段代码来调整一些换行符。
linebreak = ''
with open(path) as f:
for line in f:
line1 = line.strip()
if len(linebreak) > 0:
linebreak = linebreak + ' ' + line1
res = re.sub(r'"[^"]+"', lambda m: m.group(0).replace("\n", ""), linebreak)
if (Counter(linebreak)['"'] % 2) == 0:
linebreak = ''
print(res)
if (Counter(line1)['"'] % 2) != 0:
nextline = next(f, None).strip()
linebreak = line1 + ' ' + nextline
res = re.sub(r'"[^"]+"', lambda m: m.group(0).replace("\n", ""), linebreak)
if (Counter(linebreak)['"'] % 2) == 0:
linebreak = ''
print(res)
if (Counter(line1)['"'] % 2) == 0:
print(line1)
问题是,我文件的最后一行没有打印出来,即使它与最后一个if 条件匹配。
文件:
"content of row 1 abcde" | abcde
abcde | "content of row 2
continues here"
content of row 3 | abcde
"content of row 4
continues here" | "Test
ing"
Teste1
输出:
"content of row 1 abcde" | abcde
abcde | "content of row 2 continues here"
content of row 3 | abcde
"content of row 4 continues here" | "Test ing"
预期的:
"content of row 1 abcde" | abcde
abcde | "content of row 2 continues here"
content of row 3 | abcde
"content of row 4 continues here" | "Test ing"
Teste1
另外,我会很感激任何更简单的方法来修复这个换行符!
编辑:这条线在 GNU Linux 上完成了这项工作,但我想把它保留在 Python 上
gawk -v RS='"' 'NR % 2 == 0 { gsub(/[\r\n]+/, "") } { printf("%s%s", $0, RT) }' file > file.txt
解决方案
一旦你有偶数"
,你应该继续下一行,你不必测试另一个ifs
。continue
所以,当线路完成时,你需要一个。
from collections import Counter
import re
linebreak = ''
path = "in.txt"
with open(path) as f:
for line in f:
line1 = line.strip()
if len(linebreak) > 0:
linebreak = linebreak + ' ' + line1
res = re.sub(r'"[^"]+"', lambda m: m.group(0).replace("\n", ""), linebreak)
if (Counter(linebreak)['"'] % 2) == 0:
linebreak = ''
print(res)
continue ### ADDED LINE ###
if (Counter(line1)['"'] % 2) != 0:
nextline = next(f, None).strip()
linebreak = line1 + ' ' + nextline
res = re.sub(r'"[^"]+"', lambda m: m.group(0).replace("\n", ""), linebreak)
if (Counter(linebreak)['"'] % 2) == 0:
linebreak = ''
print(res)
if (Counter(line1)['"'] % 2) == 0:
print(line1)
推荐阅读
- flutter - onEditingComplete 在 unfocus 后不被调用
- python - 将 pcolormesh 生成的 QuadMesh 转换为 RGBA 数组
- flutter - Flutter 从其祖父访问子函数
- java - 将 Java 对象属性序列化为 JSON
- android - 如何在模型中设置数据?
- azure - 从 Azure B2C 自定义策略登录中的密码控件拆分电子邮件输入和注册链接
- javascript - 如何在 jQuery 中编写这个 JavaScript 块?
- r - R树不使用所有变量(为什么?)
- python - IONOS Python 服务器会因任何更改而立即中断
- reactjs - ReactJS apolloClient 在上下文中找不到“客户端”或作为选项传入