从文本文件中删除不符合条件的行

时间:2019-02-28 20:20:19

标签: python

我有一个包含以下内容的文本文件:

========数据:00:05:08.627012 =========

1900-01-01 00:05:08.627012; 0分1.16198; 10000000.0

1900-01-01 00:05:08.627012; 1个1.16232; 10000000.0

=========数据:00:05:12.721536 =========

1900-01-01 00:05:08.627012; 0分1.16198; 10000000.0

1900-01-01 00:05:12.721536; 0分1.16209; 1000000.0

1900-01-01 00:05:08.627012; 1个1.16232; 10000000.0

我正在尝试将其转换为csv,其中每个带有分号的项目进入其自己的单元格后。这是预期结果的一个概念。enter image description here

我不想在文本文件中包含带有=符号的行。我目前正在使用以下代码:

txt_file = open('Data/Mkt_data_test.txt', 'r')
lines = txt_file.readlines()
txt_file.close()

header_line = ['Time,', 'Bid/Ask,', 'Price,', 'Volume,']

data_lines = []

for line in lines:
    if '=' not in line:
        time_data = line.split('\n')
        for time in time_data:
            data_lines.append(time+'\n')
            data_lines = [data.replace(';', ',') for data in data_lines]

finished_file = open('mktDataFormat.csv', 'w')
finished_file.writelines(header_line)
finished_file.writelines(data_lines)
finished_file.close()

这样可以正确地写出不包含等号的行,但是在文本文件中有空白行的地方带有'='的行,也只有空白行。 enter image description here

如何摆脱那些空白行?

2 个答案:

答案 0 :(得分:0)

for line in lines:
    if '=' not in line:
        time_data = line.split('\n')
        for time in time_data:
            data_lines.append(time+'\n')
        data_lines = [data.replace(';', ',') for data in data_lines]

尝试一下,让我知道

答案 1 :(得分:0)

您的问题是您的程序没有跳过空行,因此将空行视为数据。我添加了一个检查(并有点修改了您的代码),以确保没有空行。

txt_file = open('Data/Mkt_data_test.txt', 'r')
lines = txt_file.readlines()
txt_file.close()

header_line = ['Time,', 'Bid/Ask,', 'Price,', 'Volume,\n']

data_lines = []

for line in lines:
    if '=' not in line and line.strip() != "":
        line = line.replace(';', ',')
        data_lines.append(line)

 finished_file = open('mktDataFormat.csv', 'w')
 finished_file.writelines(header_line)
 finished_file.writelines(data_lines)
 finished_file.close()