[英]strip a file from empty lines
Here's my file这是我的文件
first term word1_0.1 word2_0.2
second term another word_0.45 last word_0.89
I want my out put to be like this:我希望我的输出是这样的:
first term
word1_0.1
word2_0.2
second term
another word_0.45
last word_0.89
Here's what I did:这是我所做的:
with open("education.txt",encoding="utf-8") as openfile:
for line in openfile:
if line.strip():
for part in line.split("\t"):
file = open('output.csv','a', encoding='utf-8')
file.write(("".join(part) + "\n"))
file.close()
Which gives me the following result:这给了我以下结果:
first term
word1_0.1
word2_0.2
second term
another word_0.45
last word_0.89
I can't seem to strip my final result of those empty lines, Any idea where i went wrong?我似乎无法去除那些空行的最终结果,知道我哪里出错了吗?
Strip the lines and work on only non empty lines.剥离线条并仅处理非空行。
education.txt
: education.txt
:
first term word1_0.1 word2_0.2
second term another word_0.45 last word_0.89
updated_code.py
: updated_code.py
:
with open("education.txt",encoding="utf-8") as openfile:
for line in openfile:
if line.strip()!="":
for part in line.strip().split("\t"):
file = open('output.csv','a', encoding='utf-8')
file.write(("".join(part) + "\n"))
file.close()
output.csv
: output.csv
:
first term
word1_0.1
word2_0.2
second term
another word_0.45
last word_0.89
Disclaimer: Check if education.txt
has tab ( \t
) between values.免责声明:检查education.txt
值之间是否有制表符( \t
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.