Align column names with data in a CSV file using Python

geekchic

This is the code I am using to write data to a .csv file.

with open('temp.csv', 'a') as fp:
        a = csv.writer(fp, delimiter='\t')
        data = [['faceXpos','faceYpos','faceHeight','faceWidth','imageViewHeight','imageViewWidth','tshirtXpos', 'tshirtYpos','tshirtWidth','tshirtHeight'],
                [faceXpos, faceYpos, faceHeight, faceWidth, imageViewHeight, imageViewWidth, tshirtXpos, tshirtYpos, tshirtWidth, tshirtHeight]]
        a.writerows(data)

The output looks like so:

faceXpos    faceYpos    faceHeight  faceWidth   imageViewHeight imageViewWidth  tshirtXpos  tshirtYpos  tshirtWidth tshirtHeight
118 432 84  84  568 320 13.0    136 294.0   346.0
faceXpos    faceYpos    faceHeight  faceWidth   imageViewHeight imageViewWidth  tshirtXpos  tshirtYpos  tshirtWidth tshirtHeight
117.4   433.81  82.35999    82.36   568 320 14.45   134.19  288.26  340.09

How do I align it so that the data under each column is perfectly aligned in a way that is easier to read? Desired output: (even having the data at the center of a column would be fine)

  faceXpos  faceYpos    faceHeight  faceWidth   imageViewHeight imageViewWidth  tshirtXpos  tshirtYpos  tshirtWidth tshirtHeight
  118       432         84          84          568             320             13.0        136         294.0       346.0
E.Z.

First of all what you want is a "fixed-width file", and not a CSV.

There is a module called prettytable that could help you with that:

from prettytable import PrettyTable

# Initialize the object passing the table headers
t = PrettyTable(['A', 'B', 'C'])

t.align='l'
t.border=False

t.add_row([111,222,333])
t.add_row([444,555,666])
t.add_row(['longer text',777,'even longer text here'])

print str(t)

Output:

 A            B    C
 111          222  333
 444          555  666
 longer text  777  even longer text here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Extracting variable names and data from csv file using Python

From Dev

Extracting variable names and data from csv file using Python

From Dev

Extract column data from a CSV file using Python

From Dev

Extract column data from a CSV file using Python

From Dev

exporting sas data with special characters in column names to .CSV file

From Dev

PANDAS: Wrong column names while importing data from csv file

From Dev

view the column names for CSV file?

From Dev

Sort data in CSV file to which column it belongs to using CSV lib in Python 2.7

From Dev

Specifying column to input data in csv file in python

From Dev

Adding new data column to a csv file in python

From Dev

Change values of a column in CSV file using python

From Dev

Getting a specific column in a CSV file using Python

From Dev

Change values of a column in CSV file using python

From Dev

Using Python for adding column in a CSV file

From Dev

Pandas: No column names in data file

From Dev

Pandas: No column names in data file

From Java

Wide date to long data conversion in Python Pandas using column names

From Dev

Save data to csv file using Python

From Dev

Reading column names alone in a csv file

From Dev

Save multiple arrays to a csv file with column names

From Dev

Column names from MySQL to CSV file

From Dev

Make calculation on one column in CSV file and add it into the file using python

From Dev

Format a csv date column using mysql load data in file

From Dev

create file name based on CSV column data using gawk

From Dev

how to write user-defined column names to a csv file using pandas?

From Dev

how to write user-defined column names to a csv file using pandas?

From Dev

Python before writing to CSV file check if column data is present

From Dev

Python Append Data Line By Line To Second Column Of CSV File

From Dev

Get Column from .txt File using CSV.Reader in Python

Related Related

  1. 1

    Extracting variable names and data from csv file using Python

  2. 2

    Extracting variable names and data from csv file using Python

  3. 3

    Extract column data from a CSV file using Python

  4. 4

    Extract column data from a CSV file using Python

  5. 5

    exporting sas data with special characters in column names to .CSV file

  6. 6

    PANDAS: Wrong column names while importing data from csv file

  7. 7

    view the column names for CSV file?

  8. 8

    Sort data in CSV file to which column it belongs to using CSV lib in Python 2.7

  9. 9

    Specifying column to input data in csv file in python

  10. 10

    Adding new data column to a csv file in python

  11. 11

    Change values of a column in CSV file using python

  12. 12

    Getting a specific column in a CSV file using Python

  13. 13

    Change values of a column in CSV file using python

  14. 14

    Using Python for adding column in a CSV file

  15. 15

    Pandas: No column names in data file

  16. 16

    Pandas: No column names in data file

  17. 17

    Wide date to long data conversion in Python Pandas using column names

  18. 18

    Save data to csv file using Python

  19. 19

    Reading column names alone in a csv file

  20. 20

    Save multiple arrays to a csv file with column names

  21. 21

    Column names from MySQL to CSV file

  22. 22

    Make calculation on one column in CSV file and add it into the file using python

  23. 23

    Format a csv date column using mysql load data in file

  24. 24

    create file name based on CSV column data using gawk

  25. 25

    how to write user-defined column names to a csv file using pandas?

  26. 26

    how to write user-defined column names to a csv file using pandas?

  27. 27

    Python before writing to CSV file check if column data is present

  28. 28

    Python Append Data Line By Line To Second Column Of CSV File

  29. 29

    Get Column from .txt File using CSV.Reader in Python

HotTag

Archive