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
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
=
____10with 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‘
import
6import
7=
import
9
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]
1pandas as pd
1=
pandas as pd
3pandas as pd
4pandas as pd
5pandas as pd
6pandas as pd
7
pandas as pd
8 pandas as pd
9tsv1
0 tsv1
1
tsv1
2tsv1
3=
tsv1
5tsv1
6tsv1
7
tsv1
2tsv1
9=
=
1=
____1010
tsv1
2import
0____5 =
8
=
9pd.read_csv[
0=
import
4pd.read_csv[
3=
import
9
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]
1pd.read_csv[
7pd.read_csv[
8import
5
"Documents/Customer.tsv"
0"Documents/Customer.tsv"
1=
"Documents/Customer.tsv"
3"Documents/Customer.tsv"
4=
"Documents/Customer.tsv"
6import
5
"Documents/Customer.tsv"
0"Documents/Customer.tsv"
9=
, sep
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]
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
=
____10with 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‘
import
6import
7=
import
9
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]
1pandas as pd
8 pandas as pd
9tsv1
0 tsv1
1
tsv1
2tsv1
3=
tsv1
5tsv1
6tsv1
7
tsv1
2tsv1
9=
=
1=
____1010
tsv1
2import
0____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]
08with 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]
1with 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]
46with 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"
3import
5with 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]
52with 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"
6with 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=
, sep
1with 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]
61with 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]
1Output: