TransWikia.com

Want to convert a data file into a list of lists where the semi-colon is a separator

Data Science Asked by Rupert on August 9, 2021

I’m doing my very first data science project ever and am likely to seem a bit naive, and I’ve got three .DAT files each of which I want to convert into a list of lists, and the code I’ve been using so far is

import csv
dat1Content = [i.strip().split() for i in open("./001_SALES_DATA_NNME_28012019.DAT")

but this isn’t what I want because that uses newline to break the data up into the elements of the "outermost" list, each element of which is also a list, and then it uses space to break the data up within each sub-list, but I don’t want to use space for that, I want to use semi-colon. If my way of putting it makes sense. Can anyone help me figure out a fragment of code that would do that for me? Sorry if my question is a bit of a basic one, I’m still in the process of learning the ropes for how to clean up data in Python.

EDIT: More specifically is there a way to do it in just one line; if it’s just a case of I have to write a script for it then I’m sure I can do that.

One Answer

I think I may have the answer.

import csv
dat1Content = [i.strip().split(';') for i in open("./001_SALES_DATA_NNME_28012019.DAT")

That will do it I think, although I'll have to check.

Sorry to be asking such a naive question.

Answered by Rupert on August 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP