Python中txt文件中的空白行 [英] Blank lines in txt files in Python
本文介绍了Python中txt文件中的空白行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我几乎完成了我的项目,但是仍然存在一个小问题.我想使用Python在txt文件中写入传感器值.一切正常,但有一件事情:在txt文件中,每个值之间都有空白行.真的很烦,因为我无法将值放入电子表格中.看起来像这样:
I almost finished my project, but there is still one little problem. I want to write sensor values in a txt file with Python. All is working fine but one thing : in the txt file, there're blank lines between each value. It's really annoying because I can't put the values in a spreadsheet. It looks like That:
传感器值:
2
4
6.32
1
等...
当我这样想时:
传感器值:1个2个358等等...
Sensor values: 1 2 3 5 8 etc...
这是相关代码的一部分:
Here's the part of the code concerned :
def write_data():
global file_stream
now = datetime.now()
dt_string = now.strftime("%d-%m-%Y %H_%M_%S")
file_stream = open("data " + dt_string+".txt", "w") # mode write ou append ?
write_lines()
def write_lines():
global after_id
data = arduinoData.read()
data2 = data.decode("utf-8")
file_stream.write(data2)
print(data2)
if data2 == "F":
print("Ca a marché")
stopacq()
return
after_id = root.after(10, write_lines)
谢谢
推荐答案
添加换行符
file_stream = open("data " + dt_string+".txt", "w", newline="")
这篇关于Python中txt文件中的空白行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文