python- txt组合线以字母开头
我有一个庞大的TXT文件,其中包含行数据。 有些是空白的,有些以数字(日期)和文本开头。该结构重复出现,如下面的示例所示。
我想创建一个新文件,其中所有行从日期开始,然后是由逗号分隔的文本行。
我想我需要两个变量来存储以数字和文本开头的行,然后组合文本,直到找到下一个数字为止,但是我不知道该如何。
任何帮助都很棒!谢谢!
例子:
Input
22-05-16 20:35:20
ABC:D
UP:84.4,DN:84.3,Q=93
F: 35.7967
V: 0.50642
P: 0x1
Lev: 1483.64
22-05-16 21:30:00
ABC:D
UP:84.4,DN:84.3,Q=93
F: 35.7967
V: 0.50642
P: 0x1
Lev: 1483.64
Output:
22-05-16 20:35:20, ABC:D, UP:84.4,DN:84.3,Q=93,F: 35.7967,V: 0.50642,P: 0x1,Lev: 1483.64
22-05-16 21:30:00, ABC:D, UP:84.4,DN:84.3,Q=93,F: 35.7967,V: 0.50642,P: 0x1,Lev: 1483.64
I have a huge txt file with data by lines.
There are some that are blank, some that start with a number (date) and by text. The structure repeats itself as shows in the example below.
I want to create a new file where all the lines start with the date, followed by the text lines separated by a comma.
I suppose I need two variables to store the lines that start with number and text and combine the text ones until the next number is found, but I don´t know how to to that.
Any help would be great! Thanks!
Example:
Input
22-05-16 20:35:20
ABC:D
UP:84.4,DN:84.3,Q=93
F: 35.7967
V: 0.50642
P: 0x1
Lev: 1483.64
22-05-16 21:30:00
ABC:D
UP:84.4,DN:84.3,Q=93
F: 35.7967
V: 0.50642
P: 0x1
Lev: 1483.64
Output:
22-05-16 20:35:20, ABC:D, UP:84.4,DN:84.3,Q=93,F: 35.7967,V: 0.50642,P: 0x1,Lev: 1483.64
22-05-16 21:30:00, ABC:D, UP:84.4,DN:84.3,Q=93,F: 35.7967,V: 0.50642,P: 0x1,Lev: 1483.64
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以累积所有行,直到看到空白行并遇到空白后写入累积的字符串 -
输出
You could accumulate all lines until you see a blank line and write the accumulated string once you encounter a blank -
Output