Python Forum
Reformat csv data with Python
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reformat csv data with Python
#5
A csv file is text, so can be read like a text file.
But it's better to read as a csv.reader object as each line will be split into a list
to use csv package, you do something like:
import csv

def read_file(filename):
    with open(filename, 'r') as f:
        reader = csv.reader(f)
        for row in reader:
            for col in row:
                print('{0:<15} '.format(col), end='')
            print()

if __name__ == '__main__':
    read_file('myfile.csv')
if the first row contains headers, you can skip it, or use it for format the data into a dictionary or some other structure.
Reply


Messages In This Thread
Reformat csv data with Python - by ksdst1 - Jul-23-2018, 06:25 PM
RE: Reformat csv data with Python - by Larz60+ - Jul-23-2018, 08:58 PM
RE: Reformat csv data with Python - by buran - Jul-23-2018, 09:04 PM
RE: Reformat csv data with Python - by ksdst1 - Jul-24-2018, 04:08 PM
RE: Reformat csv data with Python - by Larz60+ - Jul-24-2018, 07:14 PM
RE: Reformat csv data with Python - by ksdst1 - Jul-25-2018, 02:09 PM
RE: Reformat csv data with Python - by Larz60+ - Jul-25-2018, 04:02 PM
RE: Reformat csv data with Python - by ksdst1 - Jul-25-2018, 09:25 PM
RE: Reformat csv data with Python - by Larz60+ - Jul-25-2018, 09:39 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020