0

I have a text file which contains a text such as: "001232029890"

And I want python to take this text and make a list in this manner:

list = [001,232,029,890]

That is, to split the text file into segments of n characters (in this case 3) and to put each of those segments in a list.

WordP
  • 159
  • 4

2 Answers2

1

You can read in your file as shown in this. Afterwards you can split the text on the nth character with [line[i:i+n] for i in range(0, len(line), n)] according to this. (Since you left the two zeros in your example I'm assuming that you meant to use strings)

IsolatedSushi
  • 152
  • 2
  • 10
1
def getListOfNChars(inputString, numberOfCharacters):
    resultList = []
    for i in range(0, len(inputString), numberOfCharacters):
        resultList.append(inputString[i : i+numberOfCharacters])
    return resultList

#inputString - The String You Want To Process
#numberOfCharacters - Number Of Characters You Want To Extract At A Time
Dharman
  • 30,962
  • 25
  • 85
  • 135
CodeAllDay
  • 54
  • 10