Thursday 15 August 2013

python - Split a file based on number of occurrences of 1 in position 1 of a line -



python - Split a file based on number of occurrences of 1 in position 1 of a line -

i routinely utilize powershell split larger text or csv files in smaller files quicker processing. however, have few files come on usual format. these print files text file. each record starts single line starts 1 , there nil else on line.

what need able split file based on number of statements. so, if want split file in chunks of 3000 statements, go downwards until see 3001 occurrence of 1 in position 1 , re-create before new file. can run windows, linux or os x pretty much open split.

any ideas appreciated.

maybe seek recognizing fact there '1' plus new line?

with open(input_file, 'r') f: my_string = f.read() my_list = my_string.split('\n1\n')

separates each record list assuming has next format:

1 .... .... 1 .... .... ....

you can output each element in list separate file.

for x in range(len(my_list)): print >> str(x)+'.txt', my_list[x]

python powershell scripting

No comments:

Post a Comment