简体   繁体   English

如何使用 python 删除.dat 文件中的行之间的空间?

[英]How to remove space between rows in .dat files using python?

I am dealing with a data file, which has only two columns:我正在处理一个数据文件,它只有两列:

 1 100
 2 200
 3 300
 4 400
 5 500

 6 600
 7 700
 8 800
 9 900
10 1000

11 1100
12 1200
13 1300
.
.
. 

This file is in .dat format, which I loaded using the np.loadtxt method.该文件为.dat格式,我使用np.loadtxt方法加载。 I want to remove the space in between rows that are appearing randomly .我想删除随机出现的行之间的空格。 I cannot do it manually because there are too many of them.我不能手动完成,因为它们太多了。 So, I am wondering if I can use any method in python to perform this task.所以,我想知道是否可以使用 python 中的任何方法来执行此任务。

Please give suggestions on it.请对此提出建议。
Thank you!谢谢!

Your best bet is to use pandas.read_csv() with specific configurations.您最好的选择是使用具有特定配置的pandas.read_csv()

>>> import pandas as pd
>>> df = pd.read_csv("<your_dat_file>", delimiter=" ", header=None, skipinitialspace=True)
>>> df
     0     1
0    1   100
1    2   200
2    3   300
3    4   400
4    5   500
5    6   600
6    7   700
7    8   800
8    9   900
9   10  1000
10  11  1100
11  12  1200
12  13  1300

I actually view this as a base Python problem, and so would suggest:我实际上将此视为基本 Python 问题,因此建议:

import re

with open("data_file.txt", "r") as fin, open("data_file_out.txt", "w") as fout:
    for line in fin.readlines():
        if re.search(r'\S', line):
            fout.write(line)

The file data_file_out.txt generated by the above should contain the sams contents as your current file, with empty lines removed ("empty" being defined here as lines which have either no content or only whitespace characters).上面生成的文件data_file_out.txt应该包含 sams 内容作为您的当前文件,删除空行(“空”在这里定义为没有内容或只有空白字符的行)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM