Hướng dẫn how do i read multiple tsv files in python? - làm cách nào để đọc nhiều tệp tsv trong python?

Vì vậy, tôi có nhiều tệp TSV với định dạng sau:

a    b    c    d    e    f    g    h
a_1  b_1  c_1  d_1  e_1  f_1  g_1  h_1
a_2  b_2  c_2  d_2  e_2  f_2  g_2  h_2
.    .    .    .    .    .    .    .
.    .    .    .    .    .    .    .
.    .    .    .    .    .    .    .
a_n  b_n  c_n  d_n  e_n  f_n  g_n  h_n

(Dòng đầu tiên (a, b, ...) là tiêu đề)

Tôi muốn đọc tất cả và nếu, với mỗi dòng, một trong các cột có thuộc tính tôi muốn (giả sử nó bằng 1), tôi muốn lưu dòng đó trong một tệp TSV khác có cùng định dạng với một ở trên nhưng dữ liệu sẽ được lọc.

Tôi có mã để trích xuất dòng tôi muốn và ghi nó vào tệp TSV nhưng tôi không chắc chắn làm thế nào để đọc nhiều tệp TSV và ghi vào một tệp TSV.

Đây là những gì tôi có cho đến nay:

with open("./someDirectory/file.tsv") as in_file, 
open("newFile.tsv","w") as out_file:
first_line = True
for line in in_file:
    if first_line: #to print the titles
        print(line, file=out_file)
        first_line = False
    columns = line.split("\t")
    columnToLookAt = columns[7]
    if columnToLookAt == "1":
        print(line, file=out_file)

Vì vậy, nói rằng một số điều có như 80 tệp TSV. Cách tốt nhất để đi lặp lại qua tất cả những thứ đó và viết các dòng cần thiết cho out_file?

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Approach:

    • Nhập thư viện Pandas
    • Sau đó đọc hai tệp TSV đầu tiên và hợp nhất chúng bằng hàm pd.merge () bằng cách đặt tham số ‘ON trên cột phổ biến có trong cả hai tệp. Sau đó lưu trữ kết quả trong một khung dữ liệu mới được gọi là ‘output_df.
    • Lưu trữ các tập tin còn lại trong một danh sách.
    • Chạy một vòng lặp sẽ lặp lại trên các tên tệp này. Đọc từng tệp này từng cái một và hợp nhất chúng bằng ‘output_df, & nbsp; dataFrame
    • Lưu ‘output_df, trong tệp TSV

    Ví dụ 1:

    Trong ví dụ này, chúng tôi sẽ hợp nhất các tệp TSV bằng cách sử dụng tham gia bên trong. Chúng tôi đã lấy bốn tệp TSV cho ví dụ này như sau.

    Tệp đã sử dụng: Khách hàng.TSV, ACCCE.TSV, CHIA CHẤT.TSV, Loan.tsvCustomer.tsv, Account.tsv, Branch.tsv, Loan.tsv

    Python3

    import pandas as pd

    Các

    with open("./someDirectory/file.tsv") as in_file, 
    open("newFile.tsv","w") as out_file:
    first_line = True
    for line in in_file:
        if first_line: #to print the titles
            print(line, file=out_file)
            first_line = False
        columns = line.split("\t")
        columnToLookAt = columns[7]
        if columnToLookAt == "1":
            print(line, file=out_file)
    
    2= pd.read_csv(
    with open("./someDirectory/file.tsv") as in_file, 
    open("newFile.tsv","w") as out_file:
    first_line = True
    for line in in_file:
        if first_line: #to print the titles
            print(line, file=out_file)
            first_line = False
        columns = line.split("\t")
        columnToLookAt = columns[7]
        if columnToLookAt == "1":
            print(line, file=out_file)
    
    5, sep=____10
    with open("./someDirectory/file.tsv") as in_file, 
    open("newFile.tsv","w") as out_file:
    first_line = True
    for line in in_file:
        if first_line: #to print the titles
            print(line, file=out_file)
            first_line = False
        columns = line.split("\t")
        columnToLookAt = columns[7]
        if columnToLookAt == "1":
            print(line, file=out_file)
    
    1

    import6import7=import9

    with open("./someDirectory/file.tsv") as in_file, 
    open("newFile.tsv","w") as out_file:
    first_line = True
    for line in in_file:
        if first_line: #to print the titles
            print(line, file=out_file)
            first_line = False
        columns = line.split("\t")
        columnToLookAt = columns[7]
        if columnToLookAt == "1":
            print(line, file=out_file)
    
    1

    pandas as pd1= pandas as pd3pandas as pd4pandas as pd5pandas as pd6pandas as pd7

    pandas as pd8 pandas as pd9tsv1 0 tsv1 1

    tsv1 2tsv1 3= tsv1 5tsv1 6tsv1 7

    tsv1 2tsv1 9= =1=____1010

    tsv1 2import0____5 =8

    =9pd.read_csv(0=import4pd.read_csv(3=import9

    with open("./someDirectory/file.tsv") as in_file, 
    open("newFile.tsv","w") as out_file:
    first_line = True
    for line in in_file:
        if first_line: #to print the titles
            print(line, file=out_file)
            first_line = False
        columns = line.split("\t")
        columnToLookAt = columns[7]
        if columnToLookAt == "1":
            print(line, file=out_file)
    
    1

    pd.read_csv(7pd.read_csv(8import5

    "Documents/Customer.tsv"0"Documents/Customer.tsv"1="Documents/Customer.tsv"3"Documents/Customer.tsv"4="Documents/Customer.tsv"6import5

    "Documents/Customer.tsv"0"Documents/Customer.tsv"9=, sep1

    with open("./someDirectory/file.tsv") as in_file, 
    open("newFile.tsv","w") as out_file:
    first_line = True
    for line in in_file:
        if first_line: #to print the titles
            print(line, file=out_file)
            first_line = False
        columns = line.split("\t")
        columnToLookAt = columns[7]
        if columnToLookAt == "1":
            print(line, file=out_file)
    
    1

    Output:

    Output.tsv

    Ví dụ 2:

    Trong ví dụ này, chúng tôi sẽ hợp nhất các tệp TSV bằng cách sử dụng tham gia bên ngoài. Chúng tôi đã lấy bốn tệp TSV cho ví dụ này như sau.

    Tệp đã sử dụng: khóa học.tsv, giáo viên.tsv, tín dụng.tsv, mark.tsv Course.tsv, Teacher.tsv, Credits.tsv, Marks.tsv

    Python3

    import pandas as pd

    Các

    with open("./someDirectory/file.tsv") as in_file, 
    open("newFile.tsv","w") as out_file:
    first_line = True
    for line in in_file:
        if first_line: #to print the titles
            print(line, file=out_file)
            first_line = False
        columns = line.split("\t")
        columnToLookAt = columns[7]
        if columnToLookAt == "1":
            print(line, file=out_file)
    
    2= pd.read_csv(
    with open("./someDirectory/file.tsv") as in_file, 
    open("newFile.tsv","w") as out_file:
    first_line = True
    for line in in_file:
        if first_line: #to print the titles
            print(line, file=out_file)
            first_line = False
        columns = line.split("\t")
        columnToLookAt = columns[7]
        if columnToLookAt == "1":
            print(line, file=out_file)
    
    5, sep=____10
    with open("./someDirectory/file.tsv") as in_file, 
    open("newFile.tsv","w") as out_file:
    first_line = True
    for line in in_file:
        if first_line: #to print the titles
            print(line, file=out_file)
            first_line = False
        columns = line.split("\t")
        columnToLookAt = columns[7]
        if columnToLookAt == "1":
            print(line, file=out_file)
    
    1

    import6import7=import9

    with open("./someDirectory/file.tsv") as in_file, 
    open("newFile.tsv","w") as out_file:
    first_line = True
    for line in in_file:
        if first_line: #to print the titles
            print(line, file=out_file)
            first_line = False
        columns = line.split("\t")
        columnToLookAt = columns[7]
        if columnToLookAt == "1":
            print(line, file=out_file)
    
    1

    pandas as pd8 pandas as pd9tsv1 0 tsv1 1

    tsv1 2tsv1 3= tsv1 5tsv1 6tsv1 7

    tsv1 2tsv1 9= =1=____1010

    tsv1 2import0____5 =8

    with open("./someDirectory/file.tsv") as in_file, 
    open("newFile.tsv","w") as out_file:
    first_line = True
    for line in in_file:
        if first_line: #to print the titles
            print(line, file=out_file)
            first_line = False
        columns = line.split("\t")
        columnToLookAt = columns[7]
        if columnToLookAt == "1":
            print(line, file=out_file)
    
    38pd.read_csv(0=
    with open("./someDirectory/file.tsv") as in_file, 
    open("newFile.tsv","w") as out_file:
    first_line = True
    for line in in_file:
        if first_line: #to print the titles
            print(line, file=out_file)
            first_line = False
        columns = line.split("\t")
        columnToLookAt = columns[7]
        if columnToLookAt == "1":
            print(line, file=out_file)
    
    05pd.read_csv(3=
    with open("./someDirectory/file.tsv") as in_file, 
    open("newFile.tsv","w") as out_file:
    first_line = True
    for line in in_file:
        if first_line: #to print the titles
            print(line, file=out_file)
            first_line = False
        columns = line.split("\t")
        columnToLookAt = columns[7]
        if columnToLookAt == "1":
            print(line, file=out_file)
    
    08
    with open("./someDirectory/file.tsv") as in_file, 
    open("newFile.tsv","w") as out_file:
    first_line = True
    for line in in_file:
        if first_line: #to print the titles
            print(line, file=out_file)
            first_line = False
        columns = line.split("\t")
        columnToLookAt = columns[7]
        if columnToLookAt == "1":
            print(line, file=out_file)
    
    1

    with open("./someDirectory/file.tsv") as in_file, 
    open("newFile.tsv","w") as out_file:
    first_line = True
    for line in in_file:
        if first_line: #to print the titles
            print(line, file=out_file)
            first_line = False
        columns = line.split("\t")
        columnToLookAt = columns[7]
        if columnToLookAt == "1":
            print(line, file=out_file)
    
    46
    with open("./someDirectory/file.tsv") as in_file, 
    open("newFile.tsv","w") as out_file:
    first_line = True
    for line in in_file:
        if first_line: #to print the titles
            print(line, file=out_file)
            first_line = False
        columns = line.split("\t")
        columnToLookAt = columns[7]
        if columnToLookAt == "1":
            print(line, file=out_file)
    
    47, sep="Documents/Customer.tsv"3import5

    with open("./someDirectory/file.tsv") as in_file, 
    open("newFile.tsv","w") as out_file:
    first_line = True
    for line in in_file:
        if first_line: #to print the titles
            print(line, file=out_file)
            first_line = False
        columns = line.split("\t")
        columnToLookAt = columns[7]
        if columnToLookAt == "1":
            print(line, file=out_file)
    
    52
    with open("./someDirectory/file.tsv") as in_file, 
    open("newFile.tsv","w") as out_file:
    first_line = True
    for line in in_file:
        if first_line: #to print the titles
            print(line, file=out_file)
            first_line = False
        columns = line.split("\t")
        columnToLookAt = columns[7]
        if columnToLookAt == "1":
            print(line, file=out_file)
    
    53="Documents/Customer.tsv"6
    with open("./someDirectory/file.tsv") as in_file, 
    open("newFile.tsv","w") as out_file:
    first_line = True
    for line in in_file:
        if first_line: #to print the titles
            print(line, file=out_file)
            first_line = False
        columns = line.split("\t")
        columnToLookAt = columns[7]
        if columnToLookAt == "1":
            print(line, file=out_file)
    
    56=, sep1
    with open("./someDirectory/file.tsv") as in_file, 
    open("newFile.tsv","w") as out_file:
    first_line = True
    for line in in_file:
        if first_line: #to print the titles
            print(line, file=out_file)
            first_line = False
        columns = line.split("\t")
        columnToLookAt = columns[7]
        if columnToLookAt == "1":
            print(line, file=out_file)
    
    59=
    with open("./someDirectory/file.tsv") as in_file, 
    open("newFile.tsv","w") as out_file:
    first_line = True
    for line in in_file:
        if first_line: #to print the titles
            print(line, file=out_file)
            first_line = False
        columns = line.split("\t")
        columnToLookAt = columns[7]
        if columnToLookAt == "1":
            print(line, file=out_file)
    
    61
    with open("./someDirectory/file.tsv") as in_file, 
    open("newFile.tsv","w") as out_file:
    first_line = True
    for line in in_file:
        if first_line: #to print the titles
            print(line, file=out_file)
            first_line = False
        columns = line.split("\t")
        columnToLookAt = columns[7]
        if columnToLookAt == "1":
            print(line, file=out_file)
    
    1

    Output:

    Hướng dẫn how do i read multiple tsv files in python? - làm cách nào để đọc nhiều tệp tsv trong python?


    Làm cách nào để kết hợp nhiều tệp TSV trong Python?

    Approach:..
    Nhập thư viện Pandas ..
    Sau đó đọc hai tệp TSV đầu tiên và hợp nhất chúng bằng PD. Hợp nhất () hàm bằng cách đặt tham số 'BẬT' thành cột chung có trong cả hai tệp. ....
    Lưu trữ các tệp còn lại trong danh sách ..
    Chạy một vòng lặp sẽ lặp lại trên các tên tệp này. ....
    Lưu 'output_df' trong tệp TSV ..

    Làm cách nào để đọc tệp .tsv trong Python?

    Cách đọc tệp TSV trong Python..
    tsv_file = open ("example.tsv").
    read_tsv = csv.đầu đọc (tsv_file, delimiter = "\ t").
    Đối với hàng trong read_tsv:.
    print(row).
    TSV_FILE.gần().

    Làm cách nào để kết hợp các tệp .tsv?

    Làm thế nào để hợp nhất các tập tin TSV trực tuyến..
    Chọn hoặc thả tài liệu TSV của bạn để tải lên để hợp nhất ..
    Sau khi tải lên hoàn tất, kéo hình thu nhỏ tài liệu TSV để sắp xếp lại chúng (nếu cần) ..
    Nhấp vào nút Hợp nhất ngay để bắt đầu quá trình hợp nhất ..
    Khi tài liệu TSV của bạn được hợp nhất, nhấp vào nút tải xuống ..

    Làm cách nào để đọc nhiều bộ dữ liệu trong gấu trúc?

    Ví dụ: Đọc nhiều tệp CSV bằng gấu trúc và sau đó tạo khung dữ liệu bằng PD.DataFrame (), kết hợp từng dataFrame vào một khung dữ liệu chính bằng PD.Concat (), sau đó chuyển đổi DataFrame chính cuối cùng thành tệp CSV bằng phương thức TO_CSV () có tên của tệp CSV mới mà chúng tôi muốn tạo như một đối số.create a data frame using pd. DataFrame(), concatenate each dataframe into a main dataframe using pd. concat(), then convert the final main dataframe into a CSV file using to_csv() method which takes the name of the new CSV file we want to create as an argument.