从第2行或跳过标题行读取文件。

时间:2022-03-14 13:39:25

How can I skip the header row and start reading a file from line2?

如何跳过标题行并开始从line2中读取文件?

7 个解决方案

#1


292  

with open(fname) as f:
    next(f)
    for line in f:
        #do something

#2


64  

f = open(fname,'r')
lines = f.readlines()[1:]
f.close()

#3


18  

If you want the first line and then you want to perform some operation on file this code will helpful.

如果你想要第一行,然后你想对文件执行一些操作,这段代码会很有帮助。

with open(filename , 'r') as f:
    first_line = f.readline()
    for line in f:
            # Perform some operations

#4


8  

f = open(fname).readlines()
firstLine = f.pop(0) #removes the first line
for line in f:
    ...

#5


2  

If slicing could work on iterators...

如果切片可以在迭代器上工作…

from itertools import islice
with open(fname) as f:
    for line in islice(f, 1, None):
        pass

#6


2  

 with open('old.csv', 'r') as f, open('new.csv', 'w') as ff:
    first_line = f.readline()
    for line in f:
        line = line.translate({ord(i):None for i in 'abcd'})
        ff.write(line)
    ff.seek(0)
    ff.write(first_line)

#7


0  

Open a connection to the file

with open('world_dev_ind.csv') as file:

打开(world_dev_ind.csv)文件:

# Skip the column names
file.readline()

# Initialize an empty dictionary: counts_dict
counts_dict = {}

# Process only the first 1000 rows
for j in range(0, 1000):

    # Split the current line into a list: line
    line = file.readline().split(',')

    # Get the value for the first column: first_col
    first_col = line[0]

    # If the column value is in the dict, increment its value
    if first_col in counts_dict.keys():
        counts_dict[first_col] += 1

    # Else, add to the dict and set value to 1
    else:
        counts_dict[first_col] = 1

Print the resulting dictionary

print(counts_dict)

打印(counts_dict)

#1


292  

with open(fname) as f:
    next(f)
    for line in f:
        #do something

#2


64  

f = open(fname,'r')
lines = f.readlines()[1:]
f.close()

#3


18  

If you want the first line and then you want to perform some operation on file this code will helpful.

如果你想要第一行,然后你想对文件执行一些操作,这段代码会很有帮助。

with open(filename , 'r') as f:
    first_line = f.readline()
    for line in f:
            # Perform some operations

#4


8  

f = open(fname).readlines()
firstLine = f.pop(0) #removes the first line
for line in f:
    ...

#5


2  

If slicing could work on iterators...

如果切片可以在迭代器上工作…

from itertools import islice
with open(fname) as f:
    for line in islice(f, 1, None):
        pass

#6


2  

 with open('old.csv', 'r') as f, open('new.csv', 'w') as ff:
    first_line = f.readline()
    for line in f:
        line = line.translate({ord(i):None for i in 'abcd'})
        ff.write(line)
    ff.seek(0)
    ff.write(first_line)

#7


0  

Open a connection to the file

with open('world_dev_ind.csv') as file:

打开(world_dev_ind.csv)文件:

# Skip the column names
file.readline()

# Initialize an empty dictionary: counts_dict
counts_dict = {}

# Process only the first 1000 rows
for j in range(0, 1000):

    # Split the current line into a list: line
    line = file.readline().split(',')

    # Get the value for the first column: first_col
    first_col = line[0]

    # If the column value is in the dict, increment its value
    if first_col in counts_dict.keys():
        counts_dict[first_col] += 1

    # Else, add to the dict and set value to 1
    else:
        counts_dict[first_col] = 1

Print the resulting dictionary

print(counts_dict)

打印(counts_dict)