0

Ok I have this part of code:

def Reading_Old_File(self, Path, turn_index, SKU):
        print "Reading Old File! Turn Index = ", turn_index, "SKU= ", SKU
        lenght_of_array=0
        array_with_data=[]
        if turn_index==1:
            reading_old_file = open(Path,'rU')
            data=np.genfromtxt(reading_old_file, delimiter="''", dtype=None)
            for index, line_in_data in enumerate(data, start=0):
                if index<3:
                    print index, "Not Yet"
                if index>=3:
                    print ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Reading All Old Items"
                    i=index-3
                    old_items_data[i]=line_in_data.split("\t")
                    old_items_data[i]=[lines_old.strip()for lines_old in old_items_data]
                    print old_items_data[i]
                    print len(old_items_data)

So what I am doing here is, I'm reading a file, on my first turn, I want to read it all, and keep all data, so it would be something like:

old_items_data[1]=['123','dog','123','dog','123','dog']
old_items_data[2]=['124','cat','124','cat','124','cat']
old_items_data[n]=['amount of list members is equal each time']

each line of the file should be stored in list, so I can use it in future for comparing, when turn_index will be greater than 2 I'll compare coming line with lines in every list(array) by iterating over all lists. So question is how do I do it, or is there any better way to compare lists? I'm new to python so maybe someone could help me with this issue?

Thanks

2
  • Where are you declaring old_items_data? It seems like you just start putting items in a list that you never created. Commented Dec 12, 2014 at 17:36
  • Yeah looks like i've deleted it, but i'm going to add old_items_data=[] after function Reading_Old_File starts Commented Dec 12, 2014 at 17:54

2 Answers 2

3

You just need to use append.

old_items_data.append(line_in_data.split("\t"))
Sign up to request clarification or add additional context in comments.

2 Comments

Will I then be able to access them all, whenever I need them? I thought old values will be overwritten, no?
append adds them to the end of the list, which is exactly what you want. If you create an empty list old_items_data = [] and try to access indices like old_items_data[0] you will get index out of bounds errors.
-1

I would use the package pandas for this. It will not only be much quicker, but also simpler. Use pandas.read_table to import the data (specifying delimiter and row-skipping can be done here by passing arguments to sep and skiprows). Then, use pandas.DataFrame.apply to apply your function to the rows of your data.

The speed gains are going to come from the fact that pandas was optimized to perform actions across lists like this (in the case of a pandas DataFrame, these would be called rows). This applies to both importing the data and applying a function to every row. The simplicity gains should hopefully be clear.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.