Gonzalo68
Gonzalo68

Reputation: 429

Parsing .DAT file with Python

I need to convert a .dat file that's in a specific format into a .csv file.

The .dat file has multiple rows with a repeating structure. The data is held in brackets and have tags. Below is the sample data; it repeats throughout the data file:

{"name":"ABSDSDSRF","ID":"AFJDKGFGHF","lat":37,"lng":-122,"type":0,"HAC":5,"verticalAccuracy":4,"course":266.8359375,"area":"san_francisco"}

Can anyone provide a starting point for the script?

Upvotes: 0

Views: 12856

Answers (4)

wwii
wwii

Reputation: 23783

Use a regex to find all of the data items. Use ast.literal_eval to convert each data item into a dictionary. Collect the items in a list.

import re, ast
result = []
s = '''{"name":"ABSDSDSRF","ID":"AFJDKGFGHF","lat":37,"lng":-122,"type":0,"HAC":5,"verticalAccuracy":4,"course":266.8359375,"area":"san_francisco"}'''

item = re.compile(r'{[^}]*?}')
for match in item.finditer(s):
    d = ast.literal_eval(match.group())
    result.append(d)

If each data item is on a separate line in the file You don't need the regex - you can just iterate over the file.

with open('file.dat') as f:
    for line in f:
        line = line.strip()
        line = ast.literal_eval(line)
        result.append(line)

Upvotes: 1

Cody Bouche
Cody Bouche

Reputation: 955

This will create a csv assuming each line in your .DAT is json. Just order the header list to your liking

import csv, json

header = ['ID', 'name', 'type', 'area', 'HAC', 'verticalAccuracy', 'course', 'lat', 'lng']

with open('file.DAT') as datfile:
    with open('output.csv', 'wb') as csvfile:
        writer = csv.DictWriter(csvfile, fieldnames=header)
        writer.writeheader()
        for line in datfile:
            writer.writerow(json.loads(line))

Upvotes: 3

kamy22
kamy22

Reputation: 143

Your row is in json format. So, you can use:

import json
data = json.loads('{"name":"ABSDSDSRF","ID":"AFJDKGFGHF","lat":37,"lng":-122,"type":0,"HAC":5,"verticalAccuracy":4,"course":266.8359375,"area":"san_francisco"}')

print data.get('name')
print data.get('ID')

This is only a start point. You have to iter all the .dat file. At the end, you have to write an exporter to save the data into the csv file.

Upvotes: 2

daniel kullmann
daniel kullmann

Reputation: 14023

Use json.load:

import json
with open (filename) as fh:
  data = json.load (fh)
   ...

Upvotes: 0

Related Questions