Hướng dẫn read csv file from second line in python - đọc tệp csv từ dòng thứ hai trong python

Trong Python 2.7.3, làm thế nào tôi có thể bắt đầu vòng lặp từ hàng thứ hai? ví dụ.

first_row = cvsreader.next[];
for row in ???: #expect to begin the loop from second row
    blah...blah...

Đã hỏi ngày 3 tháng 5 năm 2013 lúc 2:00May 3, 2013 at 2:00

GulearngulearnGuLearn

1.7541 Huy hiệu vàng17 Huy hiệu bạc31 Huy hiệu đồng1 gold badge17 silver badges31 bronze badges

6

first_row = next[csvreader]  # Compatible with Python 3.x [also 2.7]
for row in csvreader:  # begins with second row
    # ...

Kiểm tra nó thực sự hoạt động:

>>> import csv
>>> csvreader = csv.reader[['first,second', '2,a', '3,b']]
>>> header = next[csvreader]
>>> for line in csvreader:
    print line
['2', 'a']
['3', 'b']

Jamylak

124K29 Huy hiệu vàng227 Huy hiệu bạc229 Huy hiệu Đồng29 gold badges227 silver badges229 bronze badges

Đã trả lời ngày 3 tháng 5 năm 2013 lúc 2:05May 3, 2013 at 2:05

Wesley Baughwesley BaughWesley Baugh

3.6503 huy hiệu vàng22 Huy hiệu bạc42 Huy hiệu đồng3 gold badges22 silver badges42 bronze badges

1

next[reader, None] # Don't raise exception if no line exists

Có vẻ dễ đọc nhất IMO

Giải pháp thay thế khác là

from itertools import islice
for row in islice[reader, 1, None]

Tuy nhiên, bạn không nên sử dụng tiêu đề? Hãy xem xét một

first_row = next[csvreader]  # Compatible with Python 3.x [also 2.7]
for row in csvreader:  # begins with second row
    # ...
2 mà theo mặc định, đặt các tên trường thành dòng đầu tiên.

Đã trả lời ngày 3 tháng 5 năm 2013 lúc 2:35May 3, 2013 at 2:35

Jamylakjamylakjamylak

124K29 Huy hiệu vàng227 Huy hiệu bạc229 Huy hiệu Đồng29 gold badges227 silver badges229 bronze badges

1

Đã trả lời ngày 3 tháng 5 năm 2013 lúc 2:05

import csv
for field in csv.DictReader[open["./lists/SP500.csv", 'rb']]:
    symbol = [field['ticker']].rstrip[]

Wesley Baughwesley BaughMar 7, 2017 at 19:15

3.6503 huy hiệu vàng22 Huy hiệu bạc42 Huy hiệu đồngPedro Lobito

Có vẻ dễ đọc nhất IMO30 gold badges239 silver badges259 bronze badges

Trong bài viết này, chúng tôi sẽ tìm hiểu làm thế nào người ta có thể đọc một tệp từ dòng thứ hai trong Python. Chúng tôi sẽ sử dụng một số chức năng tích hợp, một số phương pháp đơn giản và một số mã tùy chỉnh cũng để hiểu rõ hơn về chủ đề.

Python xử lý các hoạt động tập tin khác nhau. Trong trường hợp đọc các tệp, người dùng có thể bắt đầu đọc một tệp từ dòng đầu tiên hoặc từ dòng thứ hai. Bài viết này sẽ cho thấy cách bạn có thể bỏ qua hàng tiêu đề hoặc dòng đầu tiên và bắt đầu đọc một tệp từ dòng 2. Hãy để chúng tôi thảo luận về bốn phương pháp khác nhau để đọc một tệp từ dòng 2. Chúng tôi sẽ đọc một tệp mẫu.txt cũng như Tệp mẫu.csv.sample.txt file as well as a sample.csv file.

Tệp văn bản mẫu //sample.txt//sample.txt

Student Details of Class X
David, 18, Science
Amy, 19, Commerce
Frank, 19, Commerce
Mark, 18, Arts
John, 18, Science

Tệp CSV mẫu //sample.csv//sample.csv

Student Details of Class X
David  18 Science
Amy    19 Commerce
Frank  19 Commerce
Mark   18 Arts
John   18 Science

Bây giờ, chúng ta hãy xem bốn cách khác nhau để đọc tệp văn bản và tệp CSV từ dòng 2 trong Python. Chúng tôi sẽ sử dụng các tệp trên để đọc nội dung.

Ví dụ: Đọc tệp văn bản từ dòng 2 bằng cách sử dụng tiếp theo []

Chúng tôi sử dụng tệp mẫu.txt để đọc nội dung. Phương thức này sử dụng Next [] để bỏ qua tiêu đề và bắt đầu đọc tệp từ dòng 2.sample.txt file to read the contents. This method uses next[] to skip the header and starts reading the file from line 2.

Lưu ý: Nếu bạn muốn in tiêu đề sau, thay vì tiếp theo [f] sử dụng

first_row = next[csvreader]  # Compatible with Python 3.x [also 2.7]
for row in csvreader:  # begins with second row
    # ...
3 và lưu trữ nó dưới dạng biến hoặc sử dụng
first_row = next[csvreader]  # Compatible with Python 3.x [also 2.7]
for row in csvreader:  # begins with second row
    # ...
4. Điều này cho thấy tiêu đề của tệp được lưu trữ trong Next [].
If you want to print the header later, instead of next[f] use
first_row = next[csvreader]  # Compatible with Python 3.x [also 2.7]
for row in csvreader:  # begins with second row
    # ...
3 and store it as a variable or use
first_row = next[csvreader]  # Compatible with Python 3.x [also 2.7]
for row in csvreader:  # begins with second row
    # ...
4. This shows that the header of the file is stored in next[].

#opens the file
with open["sample.txt"] as f:
    #start reading from line 2
    next[f]
    for line in f:
        print[line]

#closes the file
f.close[]


David, 18, Khoa học Amy, 19, Thương mại Frank, 19, Thương mại Mark, 18, Nghệ thuật John, 18, Khoa học
Amy, 19, Commerce
Frank, 19, Commerce
Mark, 18, Arts
John, 18, Science

Ví dụ: Đọc tệp CSV từ dòng 2

Chúng tôi sử dụng tệp mẫu.csv để đọc nội dung. Phương thức này đọc tệp từ dòng 2 bằng cách sử dụng

>>> import csv
>>> csvreader = csv.reader[['first,second', '2,a', '3,b']]
>>> header = next[csvreader]
>>> for line in csvreader:
    print line
['2', 'a']
['3', 'b']
1 bỏ qua tiêu đề bằng
>>> import csv
>>> csvreader = csv.reader[['first,second', '2,a', '3,b']]
>>> header = next[csvreader]
>>> for line in csvreader:
    print line
['2', 'a']
['3', 'b']
2 và in các hàng từ dòng 2. Phương pháp này cũng có thể hữu ích trong khi đọc nội dung của nhiều tệp CSV.sample.txt file to read the contents. This method uses
first_row = next[csvreader]  # Compatible with Python 3.x [also 2.7]
for row in csvreader:  # begins with second row
    # ...
5 to skip the header and starts reading the file from line 2.
first_row = next[csvreader]  # Compatible with Python 3.x [also 2.7]
for row in csvreader:  # begins with second row
    # ...
5 uses the slicing technique. As you can see in the below example,
first_row = next[csvreader]  # Compatible with Python 3.x [also 2.7]
for row in csvreader:  # begins with second row
    # ...
7 , it denotes that the reading of the file starts from index 1 as it skips the index 0. This is a much more powerful solution as it generalizes to any line. The drawback of this method is that it works fine for small files but can create problems for large files. Also, it uses unnecessary space because slice builds a copy of the contents.

#opens the file
f = open["sample.txt",'r']

#skips the header
lines = f.readlines[][1:]
print[lines]

#closes the file
f.close[]


.

Ví dụ: Đọc tệp văn bản từ dòng 2 bằng islice []

Chúng tôi sử dụng tệp mẫu.txt để đọc nội dung. Phương pháp này nhập

first_row = next[csvreader]  # Compatible with Python 3.x [also 2.7]
for row in csvreader:  # begins with second row
    # ...
8 từ mô -đun
first_row = next[csvreader]  # Compatible with Python 3.x [also 2.7]
for row in csvreader:  # begins with second row
    # ...
9 trong Python.
>>> import csv
>>> csvreader = csv.reader[['first,second', '2,a', '3,b']]
>>> header = next[csvreader]
>>> for line in csvreader:
    print line
['2', 'a']
['3', 'b']
0 có ba đối số. Đối số đầu tiên là tệp để đọc dữ liệu, thứ hai là vị trí từ đó việc đọc tệp sẽ bắt đầu và đối số thứ ba không có gì đại diện cho bước. Đây là một cách hiệu quả và pythonic để giải quyết vấn đề và có thể được mở rộng đến một số lượng các dòng tiêu đề tùy ý. Điều này thậm chí còn hoạt động cho các tệp được tải lên trong bộ nhớ trong khi lặp lại các đối tượng tệp.sample.txt file to read the contents. This method imports
first_row = next[csvreader]  # Compatible with Python 3.x [also 2.7]
for row in csvreader:  # begins with second row
    # ...
8 from
first_row = next[csvreader]  # Compatible with Python 3.x [also 2.7]
for row in csvreader:  # begins with second row
    # ...
9 module in Python.
>>> import csv
>>> csvreader = csv.reader[['first,second', '2,a', '3,b']]
>>> header = next[csvreader]
>>> for line in csvreader:
    print line
['2', 'a']
['3', 'b']
0 takes three arguments. The first argument is the file to read the data, the second is the position from where the reading of the file will start and the third argument is None which represents the step. This is an efficient and pythonic way of solving the problem and can be extended to an arbitrary number of header lines. This even works for in-memory uploaded files while iterating over file objects.

first_row = next[csvreader]  # Compatible with Python 3.x [also 2.7]
for row in csvreader:  # begins with second row
    # ...
0


David, 18, Khoa học Amy, 19, Thương mại Frank, 19, Thương mại Mark, 18, Nghệ thuật John, 18, Khoa học
Amy, 19, Commerce
Frank, 19, Commerce
Mark, 18, Arts
John, 18, Science

Ví dụ: Đọc tệp CSV từ dòng 2

Chúng tôi sử dụng tệp mẫu.csv để đọc nội dung. Phương thức này đọc tệp từ dòng 2 bằng cách sử dụng

>>> import csv
>>> csvreader = csv.reader[['first,second', '2,a', '3,b']]
>>> header = next[csvreader]
>>> for line in csvreader:
    print line
['2', 'a']
['3', 'b']
1 bỏ qua tiêu đề bằng
>>> import csv
>>> csvreader = csv.reader[['first,second', '2,a', '3,b']]
>>> header = next[csvreader]
>>> for line in csvreader:
    print line
['2', 'a']
['3', 'b']
2 và in các hàng từ dòng 2. Phương pháp này cũng có thể hữu ích trong khi đọc nội dung của nhiều tệp CSV.sample.csv file to read the contents. This method reads the file from line 2 using
>>> import csv
>>> csvreader = csv.reader[['first,second', '2,a', '3,b']]
>>> header = next[csvreader]
>>> for line in csvreader:
    print line
['2', 'a']
['3', 'b']
1 that skips the header using
>>> import csv
>>> csvreader = csv.reader[['first,second', '2,a', '3,b']]
>>> header = next[csvreader]
>>> for line in csvreader:
    print line
['2', 'a']
['3', 'b']
2 and prints the rows from line 2. This method can also be useful while reading the content of multiple CSV files.

first_row = next[csvreader]  # Compatible with Python 3.x [also 2.7]
for row in csvreader:  # begins with second row
    # ...
1


['David', '18', 'Khoa học'] ['Amy', '19', 'Thương mại'] ['Frank', '19', 'Thương mại'] '] [' John ',' 18 ',' Khoa học ']]
['Amy', '19', 'Commerce']
['Frank', '19', 'Commerce']
['Mark', '18', 'Arts']
['John', '18', 'Science']

Sự kết luận

Trong bài viết này, chúng tôi đã học đọc nội dung tệp từ dòng 2 bằng cách sử dụng một số hàm tích hợp như

>>> import csv
>>> csvreader = csv.reader[['first,second', '2,a', '3,b']]
>>> header = next[csvreader]
>>> for line in csvreader:
    print line
['2', 'a']
['3', 'b']
2,
first_row = next[csvreader]  # Compatible with Python 3.x [also 2.7]
for row in csvreader:  # begins with second row
    # ...
5,
>>> import csv
>>> csvreader = csv.reader[['first,second', '2,a', '3,b']]
>>> header = next[csvreader]
>>> for line in csvreader:
    print line
['2', 'a']
['3', 'b']
0,
>>> import csv
>>> csvreader = csv.reader[['first,second', '2,a', '3,b']]
>>> header = next[csvreader]
>>> for line in csvreader:
    print line
['2', 'a']
['3', 'b']
6 và các ví dụ khác nhau để bỏ qua dòng tiêu đề từ các tệp đã cho.

Bài Viết Liên Quan

Chủ Đề