Code Monkey home page Code Monkey logo

Comments (2)

dfurtado avatar dfurtado commented on June 6, 2024

Hi @hhoeflin , thank you so much.

Could you provide me with some examples of this use case?

Correct me if I'm wrong, the scenario that you are describing is that you want to modify a value while reading it from the CSV file, is that right?
If that's the case, would work to implement that in the __post_init__ method of the data class?
For example, let's say I always want to multiply the numbers I read from a CSV by 2, I could do something like:

CSV:

val
2

Code:

from dataclasses import dataclass
from dataclass_csv import DataclassReader


@dataclass
class Number:
    val: int

    def __post_init__(self):
        self.val = self.val * 2

def main():

    with open("numbers.csv") as f:
        reader = DataclassReader(f, Number)
        for number in reader:
            print(number)


if __name__ == "__main__":
    main()

Output:

Number(val=4)

from dataclass-csv.

hhoeflin avatar hhoeflin commented on June 6, 2024

from dataclass-csv.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.