1

I've got a raw text file with a line-separated list of EAN numbers, which I'm adding to a list (as a string) as follows:

listofEAN = []
with open('Data', newline='\r\n') as inputfile:
    for row in csv.reader(inputfile):
        listofEAN.append(row)

This creates a "list of lists" (I'm not sure why it doesn't create a single list?) in the format:

[['0075678660924'], ['0093624912613'], ['3299039990322'], ['0190295790394'], ['0075678660627'], ['0075678661150'], ...]

I'm trying to do transform the list into a list of lists with 10 EAN's each. So running listofEAN[0] would return the first 10 EAN's, and so forth.

I'm afraid I'm struggling to do this - I presume I need to use a loop of some kind, but I'm having trouble with creating a loop and combining the loop operations with the list syntax.

Any pointers would be greatly appreciated.

EDIT: Similar to the query here: How do you split a list into evenly sized chunks? but the corrections here to the way I'm importing the list is of particular interest. Thank you!

jbentley
  • 163
  • 4
  • 13
  • Row seems like it must not be just a string, it is instead a list of 1 string. IF you know it will not be empty you can just access it as the [0] element in the array – Tyler Nichols Sep 11 '17 at 12:44
  • What does the file `Data` look like? – Code-Apprentice Sep 11 '17 at 12:45
  • 1
    `listofEAN += row` would give you a flat list. (not really an answer to your question) – Stael Sep 11 '17 at 12:46
  • Ah - `Data` is a raw file of EAN numbers, each separated by a newline (it's simply formatted like this as this is how it's delivered to me) – jbentley Sep 11 '17 at 12:46
  • Possible duplicate of [How do you split a list into evenly sized chunks?](https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks) – Caramiriel Sep 11 '17 at 12:49
  • Thanks @Caramiriel - edited question with brief explanation – jbentley Sep 11 '17 at 12:55

1 Answers1

1

A row in the CSV file is always a list, even for a file with just one column. You don't really need to use a CSV reader when you have just one value per line; just strip the whitespace from each line to create a flat list, and use the standard universal newline support:

with open('Data') as inputfile:
    # create a list of non-empty lines, with whitespace removed
    stripped = (line.strip() for line in inputfile)
    listofEAN = [line for line in stripped if line]

Now it is trivial to make that into groups of a fixed size:

per_ten = [listofEAN[i:i + 10] for i in range(0, len(listofEAN), 10)]
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Is there a need for `stripped`? how about `listofEAN = [line.strip() for line in lines if not line.isspace()]`? – Chris_Rands Sep 11 '17 at 12:52
  • 1
    @Chris_Rands: That's just a different variant; both are valid approaches. Mine avoids making an extra method call. – Martijn Pieters Sep 11 '17 at 12:56
  • Ah - this looks like it could work! Thanks for the correction re: the way the raw text file is being imported - I think the single columnar format threw be a bit. – jbentley Sep 11 '17 at 12:56
  • I'm getting the error `NameError: name 'lines' is not defined` - do I need to specify any code previously to define 'lines'? – jbentley Sep 11 '17 at 13:02
  • Works perfectly! Thank you – jbentley Sep 11 '17 at 13:07