2

我有一个 .txt 文件,如下所示:

# Explanatory text
# Explanatory text
# ID_1 ID_2
10310   34426
104510  4582343
1032410 5424233
12410   957422

在文件中,同一行的两个 ID 用制表符分隔,制表符编码为'\t'

我正在尝试使用数据集中的数字进行一些分析,因此想要删除前三行。如何在 Python 中做到这一点?即我想生成一个新的数据集,如下所示:

10310   34426
104510  4582343
1032410 5424233
12410   957422

我已经尝试了以下代码,但它没有工作:

f = open(filename,'r')
lines = f.readlines()[3:]
f.close()

它不起作用,因为我得到了这种格式(一个列表,存在 \t 和 \n),而不是我在上面指出的那个:

[10310\t34426\n', '104510\t4582343\n', '1032410\t5424233\n' ... ]

4

3 回答 3

0

好的,这是解决方案:

with open('file.txt') as f:
    lines = f.readlines()

lines = lines[3:]

删除评论

此功能删除所有注释行

def remove_comments(lines):
    return [line for line in lines if line.startswith("#") == False]

删除 n 条顶行

def remove_n_lines_from_top(lines, n):
    if n <= len(lines):
        return lines[n:]
    else:
        return lines

这是完整的来源:

with open('file.txt') as f:
    lines = f.readlines()


def remove_comments(lines):
    return [line for line in lines if line.startswith("#") == False]

def remove_n_line(lines, n):
    return lines[n if n<= len(lines) else 0:]

lines = remove_n_lines_from_top(lines, 3)

f = open("new_file.txt", "w+") # save on new_file
f.writelines(lines)
f.close()
于 2020-10-20T15:01:11.677 回答
0

您可以使用 Python 的Pandas轻松完成这些任务:

import pandas as pd

pd.read_csv(filename, header=None, skiprows=[0, 1, 2], sep='\t')
于 2020-10-18T16:25:44.410 回答
0

你可以尝试这样的事情

with open(filename,'r') as fh

    for curline in fh:

         # check if the current line
         # starts with "#"

         if curline.startswith("#"):
            ...
            ...
         else:
            ...
            ...
于 2020-10-18T16:21:07.333 回答