0

I have been tasked with taking data from two csv's combining them and then inserting them into a database. The code to insert into the database work's fine, but it uses the header of the csv and matches it to columns in the database. Essentially I need to take a list of the columns and the database and a list of the headers in the csvs and generate a a new csv that that takes the data from the old csvs and put the new headers onto the merged csv(note that there is also some logic performed, such as combining columns, so it is not just a merge). I am wondering if there is a way to simultaneously declare class variables and put them into a list, while leaving other class variables outside of the list. This would make it easier to insert the new header to the new csv. I want something like the following:

class MyClass():
    HEADER_FOR_NEW_CSV=[
        HEADER_VALUE_THAT_IS_ALSO_A_CLASS_VARIABLE="",
        ANOTHER_CLASS_VARIABLE=""
    ]
    CSV_INPUT_FILE_NAME=""
    OTHER_CLASS_VARIABLES=""
    HEADER_FROM_INPUT_CSV=[
        MORE_CLASS_VARIABLES=""
    ]

Is there a way to do this? If not is there a better way to organise my data? Or should I just declare my header variables and then make a list of them afterward?

Albert Rothman
  • 998
  • 2
  • 9
  • 27
  • have you looked at [dictionaries](https://docs.python.org/2/tutorial/datastructures.html#dictionaries)? – Olian04 Nov 04 '16 at 17:26
  • Yes, I considered using a dictionary, but I would probably have a key and value that are the same, which feels pointless, moreover, I would have to have hardcoded strings throughout my code. I want to store my class variables so that I can reuse them and change them easily(maintianability). My problem is I need to insert hardcoded strings into my output, and that's all I need the list for. With format I want, I could add header values as class variables if I need to in the future. – Albert Rothman Nov 04 '16 at 18:18
  • Again, my biggest concern is maintainability. If I can group class variables together in one place in the code it is easy add more and resuse them in the code. Otherwise I have to be careful not to forget to add them to the output csv header or a list that contains all of the header values. I hope that makes sense. – Albert Rothman Nov 04 '16 at 18:18
  • upon thinking about this problem more, I have added another class that contains just eh columns as class variables, I then add them to the the csv using that class's `__dict__` method; are there any dire consequences of this if I don't modify the `__getattr__` method? see [this](http://stackoverflow.com/questions/191010/how-to-get-a-complete-list-of-objects-methods-and-attributes) – Albert Rothman Nov 04 '16 at 18:59

0 Answers0