[Python] Why is my Python code displaying CSV file with right sided alignment?

Discussão em 'Python' iniciado por Stack, Abril 16, 2025.

  1. Stack

    Stack Membro Participativo

    I have written this code just to read a csv file:

    import pandas as pd
    df = pd.read_table('C:\\XXXXX\\Python_Learn\\pandas.csv')
    print(df.to_string())


    and output to this is coming as below:

    Date|Invoice ID|Customer Name|Product|Category|Quantity|Unit Price|Total Amount|Region|Salesperson
    0 2025-04-01|INV1001|John Smith|Apple iPhone 14|Electronics|1|999.00|999.00|North|Alice Johnson
    1 2025-04-01|INV1002|Jane Doe|Samsung TV 55|Electronics|2|600.00|1200.00|NULL|Bob Williams
    2 2025-04-02|INV1003|Michael Lee|Nike Sneakers|Apparel|3|120.00|360.00|West|Carol Chen
    3 2025-04-02|INV1004|Emma Brown|Office Chair|Furniture|1|150.00|150.00|South|David Patel
    4 2025-04-03|INV1005|Olivia Green|HP Laptop|Electronics|1|850.00|850.00|North|Alice Johnson
    5 2025-04-03|INV1006|Noah White|Dining Table|Furniture|1|450.00|450.00|NULL|Bob Williams
    6 2025-04-04|INV1007|Ava Scott|Levis Jeans|Apparel|2|80.00|160.00|West|Carol Chen
    7 2025-04-04|INV1008|Liam Davis|AirPods Pro|Electronics|2|250.00|500.00|NULL|David Patel


    Why is my output getting aligned to the right?

    I added .strip() but it still gives same output.

    Update: I updated my code with .read_csv() instead of .read_table() & it improved the output in terms of alignment, but now it doesn't show characters on the right side of the file.

    import pandas as pd
    df = pd.read_csv('C:\\XXXXX\\Python_Learn\\pandas.csv')
    print(df)


    Output as below,

    Date|Invoice ID|Customer Name|Product|Category|Quantity|Unit Price|Total Amount|Region|Salesperson
    0 2025-04-01|INV1001|John Smith|Apple iPhone 14|...
    1 2025-04-01|INV1002|Jane Doe|Samsung TV 55|Elec...
    2 2025-04-02|INV1003|Michael Lee|Nike Sneakers|A...
    3 2025-04-02|INV1004|Emma Brown|Office Chair|Fur...
    4 2025-04-03|INV1005|Olivia Green|HP Laptop|Elec...
    5 2025-04-03|INV1006|Noah White|Dining Table|Fur...
    6 2025-04-04|INV1007|Ava Scott|Levis Jeans|Appar...
    7 2025-04-04|INV1008|Liam Davis|AirPods Pro|Elec...


    Why does it show ... instead of reading content on right side of the file?

    Continue reading...

Compartilhe esta Página