Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python CSV different delimiter

I am trying to read a file that has ` as delimiters. I have tried some of the other solutions for a different delimiter but none seemed to work.

with open("data.csv", "r") as data:
    for line in data:
        for line.split('`') as element:
            print(element)

example input file:

sarah`120`18kg`22Rep
thomas`160`8kg`11Rep

the expected out should be:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

sarah
120
18kg
22Rep
thomas
160
8kg
11Rep

but this is what I get:




Thanks for your time!

>Solution :

I’d suggest using the DictReader class from the standard library like so:

import csv

def read_csv(filepath: str) -> list:
    """Read csv file from given path, return contents as list of dictionaries"""
    with open(filepath, encoding='utf-8') as f:
        r = csv.DictReader(f, delimiter='`')
        return list(r)

print(read_csv("data.csv"))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading