A convenient Python API for working with the text files CSV, JSON, TXT
The library allows handling csv, txt and json files easily from within Python.
pip install textfiles
Import TextFile: Once you install the library you can import the TextFile class into your Python code. To do this, simply add the following line to your code:
from textfiles.file_factory import TextFile
Use TextFile: You can now use the TextFile class to create instances of CsvFile, TxtFile, and JsonFile classes. The factory has two methods:
make_file_instance: This method creates an instance of the file class for an existing text file.
make_file: This method creates a new file and a corresponding Python instance of it.
To create an instance of a CsvFile, TxtFile, or JsonFile class, simply call the appropriate static method and pass in the necessary parameters. The factory will then return an instance of the appropriate class.
Examples: Here are some examples of how to use TextFile:
To create an instance of an existing file:
file = TextFile.make_file_instance('csv', 'path/to/file.csv', has_header=True, delimiter=',')
file = TextFile.make_file_instance('txt', 'path/to/file.txt')
file = TextFile.make_file_instance('json', 'path/to/file.json')
To create a new file:
file = TextFile.make_file('csv', 'path/to/file.csv', ['Header1', 'Header2'])
file = TextFile.make_file('txt', 'path/to/file.txt')
file = TextFile.make_file('json', 'path/to/file.json')
Note - When creating a CSV file, it is mandatory to pass a header.
self.get_content()
self.search()
self.count()
my_csv = CsvFile('/path/to/my/csv/file.csv')
my_csv.update_cell(column=3, row=2, value='New content')
with open ("example.json", "w") as f:
json.dump(f, {"State": "New York", "cities": ["New York", "Albeny", "New Paltz"]})
my_json = JsonFile('/path/to/my/json/file.json')
new = my_json.search("new")
print(new)
output:
[{'State': 'New York'}, {'cities': [{'index[0]': 'New York'}, {'index[2]': 'New Paltz'}]}]