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

时间:2011-01-25 17:25:09

标签: python file-io

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

8 个答案:

答案 0 :(得分:372)

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

答案 1 :(得分:83)

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

答案 2 :(得分:20)

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

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

答案 3 :(得分:8)

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

答案 4 :(得分:7)

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

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

答案 5 :(得分:0)

# Open a connection to the file
with open('world_dev_ind.csv') as file:

    # 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)

答案 6 :(得分:0)

要概括读取多个标题行的任务并提高可读性,我将使用方法提取。假设您想标记coordinates.txt的前三行以用作标题信息。

示例

coordinates.txt
---------------
Name,Longitude,Latitude,Elevation, Comments
String, Decimal Deg., Decimal Deg., Meters, String
Euler's Town,7.58857,47.559537,0, "Blah"
Faneuil Hall,-71.054773,42.360217,0
Yellowstone National Park,-110.588455,44.427963,0

然后方法提取使您可以指定要对标题信息进行的 (在本示例中,我们只是基于逗号对标题行进行标记化,并将其作为列表返回,但是仍有空间做更多)。

def __readheader(filehandle, numberheaderlines=1):
    """Reads the specified number of lines and returns the comma-delimited 
    strings on each line as a list"""
    for _ in range(numberheaderlines):
        yield map(str.strip, filehandle.readline().strip().split(','))

with open('coordinates.txt', 'r') as rh:
    # Single header line
    #print next(__readheader(rh))

    # Multiple header lines
    for headerline in __readheader(rh, numberheaderlines=2):
        print headerline  # Or do other stuff with headerline tokens

输出

['Name', 'Longitude', 'Latitude', 'Elevation', 'Comments']
['String', 'Decimal Deg.', 'Decimal Deg.', 'Meters', 'String']

如果coordinates.txt包含另一个标题行,只需更改numberheaderlines。最棒的是,很清楚__readheader(rh, numberheaderlines=2)在做什么,并且避免了必须弄清楚为什么要接受的答案的作者在其代码中使用next()的含糊之处。

答案 7 :(得分:0)

如果您想从第2行开始读取多个CSV文件,这就像超级按钮

<Grid>
    <ScrollViewer>
        <StackPanel>
            <ListBox x:Name="ListViewTableBorder" ItemsSource="{Binding TableName}" SelectionChanged="ListViewTableBorder_Selected" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Border Style="{StaticResource sales_border}">
                            <TextBlock Name="tablename" Text="{Binding tablename}" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White" FontSize="20"/>
                        </Border>
                    </DataTemplate>
                </ListBox.ItemTemplate>
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel IsItemsHost="True" Orientation="Horizontal"/>
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
            </ListBox>
        </StackPanel>
    </ScrollViewer>
</Grid>

(这是另一个问题的Parfait's answer的一部分)