Skip to content

fogonwater/melonfarmer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

49 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

melon (farmer)


Simple utilities for harvesting data + reading and writing files.

Import just what you need...

from melon import read_json, list_to_csv

... or import melon as farmer if that's how you roll.

import melon as farmer

Some example uses.

Get some json and write to a file.

url = 'https://api.github.com/users/fogonwater/repos'
d1 = farmer.get_json(url)
farmer.write_json(d1, 'data/test1.json')

Write json to file and then read it back.

farmer.write_json({'foo' : 'bar'}, 'data/test2.json')
d2 = farmer.read_json('data/test2.json')

Parse a one-line csv-like string

s = 'value1,"oh look, an embedded comma",value3'
print(farmer.parse_one_line_csv(s))

Write a list of dictionaries to a CSV file. (Assumes all dicts have identical keys.)

report = [
    {'num':0,'day':'Mon'}, 
    {'num':1,'day':'Tue'}
]
farmer.dict_to_csv(report,'data/test1.csv')

Write a list of lists to a CSV file.

report = [
    ['col_a', 'col_b', 'col_c'],
    [1, 'melon', 'yes'],
    [9, 'apple', 'no'],
    [4, 'melon', 'maybe']
]
farmer.list_to_csv(report,'data/test2.csv')

Load CSV as a list of dictionaries.

rows = farmer.read_csv('data/test2.csv')

Get a list of all .json files in a directory called 'data'.

json_files = farmer.get_filenames('data', suffix='.json')
print(json_files)

Check to see whether specific files exist.

json_files = farmer.get_filenames('data', suffix='.json')
json_files.append('red_herring.json')
for f_name in json_files:
    if farmer.file_exists(f_name):
        print("{} exists.".format(f_name))
    else:
        print("{} does not exist.".format(f_name))

About

Simple utilities for harvesting metadata.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages