Converting mixed-format .DAT to .CSV (or anything else)

后端 未结 3 386
无人及你
无人及你 2021-01-21 16:14

I have a large collection of DAT files that need to be converted (eventually to a unique file type). The DAT\'s have a mixed amount of whitespace between fields, and the column

3条回答
  •  一整个雨季
    2021-01-21 16:46

    This is answer for "Python - download and convert .dat to .csv [duplicate]". I couldn't post there so FYI you can get you exact output from here.

    import urllib2
    import csv
    url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-mpg.data'
    response = urllib2.urlopen(url)
    readData = response.read()
    strObj = filter(None,readData.splitlines())
    
    strObj = [w.replace('\t', '  ') for w in strObj]
    
    listB = []
    for i in strObj:
        listB.append(filter(None,i.split("  ")))
    with open(r'c:/data2.csv','a') as f:
        writer = csv.writer(f)
        writer.writerows(listB)
    

提交回复
热议问题