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:
sarah 120 18kg 22Rep thomas 160 8kg 11Rep
but this is what I get:
Thanks for your time!
Advertisement
Answer
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"))