1

I am working on a csv file using python.

I wrote the following script to treat the file:

import pickle
import numpy as np
from csv import reader, writer


dic1 = {'a': 2, 'b': 2, 'c': 2}
dic2 = {'a': 2,'b': 2,'c': 0}
number = dict()

for k in dic1:
    number[k] = dic1[k] + dic2[k]

ctVar = {'a': [0.093323751331788565, -1.0872670058072453, '', 8.3574590513050264], 'b': [0.053169909627947334, -1.0825742255395172, '', 8.0033788558001984], 'c': [-0.44681777279768059, 2.2380488442495348]}

Var = {}

for k in number:
    Var[k] = number[k]


def findIndex(myList, number):
    n = str(number)
    m = len(n)
    for elt in myList:
        e = str(elt)
        l = len(e)
        mi = min(m,l)
        if e[:mi-1] == n[:mi-1]:
            return myList.index(elt)

def sortContent(myList):
    if '' in myList:
        result = ['']
        myList.remove('')
    else:
        result = []

    myList.sort()
    result = myList + result
    return result

An extract of the csv file follows: (INFO: The blanks are important. To increase the readability, I noted them BL but they should just be empty cases)

The columns contain few elements (including '') repeated many times.

a

0.0933237513

-1.0872670058

0.0933237513

BL

BL

0.0933237513

0.0933237513

0.0933237513

BL

Second column:

b

0.0531699096

-1.0825742255

0.0531699096

BL

BL

0.0531699096

0.0531699096

0.0531699096

BL

Third column:

c

-0.4468177728

2.2380488443

-0.4468177728

-0.4468177728

-0.4468177728

-0.4468177728

-0.4468177728

2.2380488443

2.2380488443

I just posted an extract of the code (where I am facing a problem) and we can't see its utility. Basically, it is part of a larger code that I use to modify this csv file and encode it differently.

In this extract, I am trying at some point (line 68) to sort elements of a list that contains numbers and ''.

When I remove the line that does this, the elements printed are those of each column (without any repetition).

The problem is that, when I try to sort them, the '' are no longer taken into account. Yet, when I tested my function sortContent with lists that have '', it worked perfectly.

I thought this problem was related to the use of numpy.float64 elements in my list. So I converted all these elements into floats, but the problem remains.

Any help would be greatly appreciated!

Community
  • 1
  • 1
bigTree
  • 1,952
  • 6
  • 24
  • 42
  • 2
    Nobody wants to go to a spam-filled file-downloader site to get a zipfile full of who knows what just to answer your question. Strip down your code and sample data to something you can fit here, or at least in something like pastebin. Also, please describe what it's supposed to do. – abarnert Aug 20 '13 at 22:07
  • 1
    Do you want `sortContent` to remove `''` or not? That's clearly what its code does. – rakslice Aug 21 '13 at 00:32
  • @rakslice I want to keep ' '. I thought the first if statement would do this? (I remove '' from list to sort it later, but if myList contained a '', then it is restituted in the end) – bigTree Aug 21 '13 at 00:49
  • So what's the problem? – rakslice Aug 21 '13 at 00:59
  • the problem is that when I run the program, if I sort 'content' (content = sortContent(content)) then the ' ' is removed from the lists. However, I want to sort content and keep ' ' (the blank should appear at the end of the list). For instance, if I have a list [1, -1, ' ', 2] then I want [-1, 1, 2, ' ']. Here, what the code does is [-1, 1, 2]. Thus, I get an error (ind looks for the index of ' ' but returns None since there is no ' ') – bigTree Aug 21 '13 at 01:32

1 Answers1

0

I assume you mean to use sortContent on something else (as obviously if you want the values in your predefined lists in ctVar in a certain order, you can just put them in order in your code rather than sorting them at runtime).

Let's go through your sortContent piece by piece.

if '' in myList:
    result = ['']
    myList.remove('')

If the list object passed in (let's call this List 1) has items '', create a new list object (let's call it List 2) with just '', and remove the first instance of '' from list 1.

mylist.Sort()

Now, sort the contents of list 1.

result = myList + result

Now create a new list object (call it list 3) with the contents of list 1 and list 2.

return result

Keep in mind that list 1 (the list object that was passed in) still has the '' removed.

rakslice
  • 7,846
  • 3
  • 47
  • 51