1

Quiero leer este archivo mediante la libreria pandas y generar un dataframe. El dataframe deberia contener 4 columnas, de manera que cada "," indica una columna distinta, y cada ";" indica una fila distinta. No se como leer el fichero para generar el dataframe correspondiente.

El archivo .txt contiene lo siguiente:

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;

Muchas gracias!

1 respuesta 1

0

Puedes usar el parámetro lineterminator de pandas.read_csv para que se considere el carácter ; como fin de linea.

Hay no obstante hay un problema, sobra el ; final del archivo:

...; 533,257,33,33; 611,336,33,33; 836,1109,33,33; 
                                                 ^
                                                 ^
                                  Deberías arder en el infierno :)

Esto hace que se espere una nueva columna y el parser falla al considerar el \n final como dato de la primera columna (moraleja: no se usa un separador si no hay nada que separar). Puedes eliminarlo manualmente o cargar el archivo en memoria entero previamente y eliminarlo con str.strip.

Sería muy fácil de solucionar usando el parámetro skipfooter pero por desgracia el motor de Python no implementa a día de hoy lineterminator y el parser de C que si lo implementa no implementa skipfooter... perfecto...

import io

import pandas as pd


with open("/ruta/arcivo.txt") as file:
    data = io.StringIO(file.read().strip())

df = pd.read_csv(data, sep=",", header=None,lineterminator=";")
>>> 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
344  836  1109  33  33

[345 rows x 4 columns]

>>> df.dtypes
0    int64
1    int64
2    int64
3    int64
dtype: object
1
  • Perfecto. Funciona. Muchisimas gracias!
    – Jose David
    Commented el 26 abr. 2020 a las 16:34

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.