读取 .txt 文件并按 pandas 获取行和列

Read .txt file and get rows and columns by pandas

我想通过 pandas 库读取此文件以获取数据框。

这个数据框应该有 4 列和很多行。列分隔符 是“,”,而行分隔符是“;”。该文件是 .txt 格式,我不知道该怎么做。

.txt 文件:

822,100,33,33; 321,143,33,33; 367,236,33,33; 775,292,33,33; 951,492,33,33; 1153,518,33,33; 939,738,33,33; 988,702,33,33; 936,686,33,33; 946,647,33,33; 965,613,33,33; 900,636,33,33; 924,607,33,33; 936,565,33,33; 533,635,33,33; 515,570,33,33; 618,653,33,33; 669,620,33,33; 721,614,33,33; 759,614,33,33; 739,573,33,33; 774,576,33,33; 816,573,33,33; 851,551,33,33; 767,533,33,33; 852,475,33,33; 797,375,33,33; 704,512,33,33; 743,435,33,33; 807,446,33,33; 719,475,33,33; 638,503,33,33; 622,475,33,33; 704,409,33,33; 658,434,33,33; 660,394,33,33; 605,427,33,33; 595,397,33,33; 559,404,33,33; 577,424,33,33; 556,464,33,33; 537,434,33,33; 522,420,33,33; 479,420,33,33; 467,445,33,33; 479,504,33,33; 423,462,33,33; 431,492,33,33; 422,523,33,33; 394,558,33,33; 360,576,33,33; 363,603,33,33; 401,622,33,33; 441,631,33,33; 240,456,33,33; 287,435,33,33; 346,390,33,33; 313,338,33,33; 364,341,33,33; 411,341,33,33; 408,389,33,33; 442,280,33,33; 482,291,33,33; 521,331,33,33; 556,327,33,33; 574,287,33,33; 533,257,33,33; 611,336,33,33; 836,1109,33,33; 822,100,33,33; 321,143,33,33; 367,236,33,33; 775,292,33,33; 951,492,33,33; 1153,518,33,33; 939,738,33,33; 988,702,33,33; 936,686,33,33; 946,647,33,33; 965,613,33,33; 900,636,33,33; 924,607,33,33; 936,565,33,33; 533,635,33,33; 515,570,33,33; 618,653,33,33; 669,620,33,33; 721,614,33,33; 759,614,33,33; 739,573,33,33; 774,576,33,33; 816,573,33,33; 851,551,33,33; 767,533,33,33; 852,475,33,33; 797,375,33,33; 704,512,33,33; 743,435,33,33; 807,446,33,33; 719,475,33,33; 638,503,33,33; 622,475,33,33; 704,409,33,33; 658,434,33,33; 660,394,33,33; 605,427,33,33; 595,397,33,33; 559,404,33,33; 577,424,33,33; 556,464,33,33; 537,434,33,33; 522,420,33,33; 479,420,33,33; 467,445,33,33; 479,504,33,33; 423,462,33,33; 431,492,33,33; 422,523,33,33; 394,558,33,33; 360,576,33,33; 363,603,33,33; 401,622,33,33; 441,631,33,33; 240,456,33,33; 287,435,33,33; 346,390,33,33; 313,338,33,33; 364,341,33,33; 411,341,33,33; 408,389,33,33; 442,280,33,33; 482,291,33,33; 521,331,33,33; 556,327,33,33; 574,287,33,33; 533,257,33,33; 611,336,33,33; 836,1109,33,33; 822,100,33,33; 321,143,33,33; 367,236,33,33; 775,292,33,33; 951,492,33,33; 1153,518,33,33; 939,738,33,33; 988,702,33,33; 936,686,33,33; 946,647,33,33; 965,613,33,33; 900,636,33,33; 924,607,33,33; 936,565,33,33; 533,635,33,33; 515,570,33,33; 618,653,33,33; 669,620,33,33; 721,614,33,33; 759,614,33,33; 739,573,33,33; 774,576,33,33; 816,573,33,33; 851,551,33,33; 767,533,33,33; 852,475,33,33; 797,375,33,33; 704,512,33,33; 743,435,33,33; 807,446,33,33; 719,475,33,33; 638,503,33,33; 622,475,33,33; 704,409,33,33; 658,434,33,33; 660,394,33,33; 605,427,33,33; 595,397,33,33; 559,404,33,33; 577,424,33,33; 556,464,33,33; 537,434,33,33; 522,420,33,33; 479,420,33,33; 467,445,33,33; 479,504,33,33; 423,462,33,33; 431,492,33,33; 422,523,33,33; 394,558,33,33; 360,576,33,33; 363,603,33,33; 401,622,33,33; 441,631,33,33; 240,456,33,33; 287,435,33,33; 346,390,33,33; 313,338,33,33; 364,341,33,33; 411,341,33,33; 408,389,33,33; 442,280,33,33; 482,291,33,33; 521,331,33,33; 556,327,33,33; 574,287,33,33; 533,257,33,33; 611,336,33,33; 836,1109,33,33; 822,100,33,33; 321,143,33,33; 367,236,33,33; 775,292,33,33; 951,492,33,33; 1153,518,33,33; 939,738,33,33; 988,702,33,33; 936,686,33,33; 946,647,33,33; 965,613,33,33; 900,636,33,33; 924,607,33,33; 936,565,33,33; 533,635,33,33; 515,570,33,33; 618,653,33,33; 669,620,33,33; 721,614,33,33; 759,614,33,33; 739,573,33,33; 774,576,33,33; 816,573,33,33; 851,551,33,33; 767,533,33,33; 852,475,33,33; 797,375,33,33; 704,512,33,33; 743,435,33,33; 807,446,33,33; 719,475,33,33; 638,503,33,33; 622,475,33,33; 704,409,33,33; 658,434,33,33; 660,394,33,33; 605,427,33,33; 595,397,33,33; 559,404,33,33; 577,424,33,33; 556,464,33,33; 537,434,33,33; 522,420,33,33; 479,420,33,33; 467,445,33,33; 479,504,33,33; 423,462,33,33; 431,492,33,33; 422,523,33,33; 394,558,33,33; 360,576,33,33; 363,603,33,33; 401,622,33,33; 441,631,33,33; 240,456,33,33; 287,435,33,33; 346,390,33,33; 313,338,33,33; 364,341,33,33; 411,341,33,33; 408,389,33,33; 442,280,33,33; 482,291,33,33; 521,331,33,33; 556,327,33,33; 574,287,33,33; 533,257,33,33; 611,336,33,33; 836,1109,33,33; 822,100,33,33; 321,143,33,33; 367,236,33,33; 775,292,33,33; 951,492,33,33; 1153,518,33,33; 939,738,33,33; 988,702,33,33; 936,686,33,33; 946,647,33,33; 965,613,33,33; 900,636,33,33; 924,607,33,33; 936,565,33,33; 533,635,33,33; 515,570,33,33; 618,653,33,33; 669,620,33,33; 721,614,33,33; 759,614,33,33; 739,573,33,33; 774,576,33,33; 816,573,33,33; 851,551,33,33; 767,533,33,33; 852,475,33,33; 797,375,33,33; 704,512,33,33; 743,435,33,33; 807,446,33,33; 719,475,33,33; 638,503,33,33; 622,475,33,33; 704,409,33,33; 658,434,33,33; 660,394,33,33; 605,427,33,33; 595,397,33,33; 559,404,33,33; 577,424,33,33; 556,464,33,33; 537,434,33,33; 522,420,33,33; 479,420,33,33; 467,445,33,33; 479,504,33,33; 423,462,33,33; 431,492,33,33; 422,523,33,33; 394,558,33,33; 360,576,33,33; 363,603,33,33; 401,622,33,33; 441,631,33,33; 240,456,33,33; 287,435,33,33; 346,390,33,33; 313,338,33,33; 364,341,33,33; 411,341,33,33; 408,389,33,33; 442,280,33,33; 482,291,33,33; 521,331,33,33; 556,327,33,33; 574,287,33,33; 533,257,33,33; 611,336,33,33; 836,1109,33,33;

谢谢!

试试这个:

df = pd.read_csv('test.txt', lineterminator=';', engine='c', header=None)
print(df)

       0     1   2   3
0    822   100  33  33
1    321   143  33  33
2    367   236  33  33
3    775   292  33  33
4    951   492  33  33
..   ...   ...  ..  ..
340  556   327  33  33
341  574   287  33  33
342  533   257  33  33
343  611   336  33  33
import pandas as pd
FilePath =  'C:\temp\tstdel.txt'
df11 = pd.read_table(FilePath, lineterminator= ';', delimiter=',', header = None)
df11

给予

0 1 2 3 0 822 100 33 33 1 321 143 33 33 2 367 236 33 33 3 775 292 33 33 4 951 492 33 33 5 1153 518 33 33