0

I want to force a line break after every 10 numbers or 9 nine splits on a txt file in python? How would I go about this? So say i have

Input would be something like: 
40 20 30 50 40 40 40 40 40 40  
20 40 20 30 50 40 40 40 
20 40 20 30 50 40 40 40 40 20 20 20
20 20 20 

int a txt.file and output should be

40 20 30 50 40 40 40 40 40 40  
20 40 20 30 50 40 40 40 
20 40 20 30 50 40 40 40 40 20
20 20 
20 20 20 

so essentially break after every 10 numbers, or 9 line splits

I have tried:

with open('practice.txt') as f:

    for line in f:
         int_list = [int(num) for num in line.split() ]
         if len(int_list) < 20:
            print(int_list)
         else: 

             new_list = int_list[20:]
             int_list = int_list[:20]
             print(int_list)
             print(new_list)

But this doesn't quite solve it. Also note that the line lengths can vary. So the first line could have 5 numbers and the second line have 9 and the third 10 and the fourth 10

Kalimantan
  • 702
  • 1
  • 9
  • 28
  • 1
    Can you please add a desired input and output format for me. Please make sure to indent it by 4 spaces :) – Harrison Aug 28 '16 at 00:43
  • Do you want to write the results back out to a file or just show them on screen? And do you need to convert the values to ints, or just process the lines as you've described (add line breaks after every 10th entry on the same line). Also, does this have anything to do with pandas (as you've tagged it)? – Matthias Fripp Aug 28 '16 at 01:04
  • Since code work -- use codereview http://codereview.stackexchange.com/ – Merlin Aug 28 '16 at 01:07
  • Possible duplicate of: http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python – citaret Aug 28 '16 at 01:14
  • @mfripp well after i line break them(10 per line) I will manipulate each line and do calculations. So they do need to be cast as its for my purposes – Kalimantan Aug 28 '16 at 01:15
  • @citaret no because in that solution it assumes that all lines are of equal length, whereas here a the first line could contain 5 numbers, and the second, 10, and the third 9. The important factor is that they don't go over 10 numbers – Kalimantan Aug 28 '16 at 01:25
  • @Kalimantan Can't you apply the `chunks` function to each line even if it's shorter? – citaret Aug 28 '16 at 01:30
  • Your input and output examples don't quite match up. Is that intentional? Your input has 12 items on the last row, but the output only has 10 (it's dropped two of the 40 values). Also, if you are converting to ints, you won't preserve the extra space after the 50 in the last line. Is that OK? – Matthias Fripp Aug 28 '16 at 01:31
  • @mfripp that was unintentional sorry, sometimes formatting it in python is hard cause of the space sensitivity. But basically the biggest factor here is that at most they have 10 numbers per line but should not be grouped if they have less – Kalimantan Aug 28 '16 at 01:33
  • In your new example, why do the two 20's from the end of the third input line get added to the start of the 4th output line? Don't you want to keep all existing lines separate? – Matthias Fripp Aug 28 '16 at 01:45
  • yeah, sorry, fixed it – Kalimantan Aug 28 '16 at 01:47
  • Thanks for all the help, took me forever to get this part – Kalimantan Aug 28 '16 at 01:51

2 Answers2

2

It looks like everything below the with statement should be indented one more level. Your method seems like a good start, but it will not work if there are more than 2 groups on one line. The following code takes care of that and simplifies things a bit:

with open('practice.txt') as f:
    values = []
    for line in f:
        int_list = [int(num) for num in line.split()]
        # the next line splits int_list into groups of 10 items, 
        # and appends all the groups to the values list
        values.extend(int_list[i:i+10] for i in range(0, len(int_list), 10))

print values
# [
#     [40, 20, 30, 50, 40, 40, 40, 40, 40, 40],
#     [20, 40, 20, 30, 50, 40, 40, 40],
#     [20, 40, 20, 30, 50, 40, 40, 40, 40, 20],
#     [20, 20],
#     [20, 20, 20]
# ]
Matthias Fripp
  • 17,670
  • 5
  • 28
  • 45
  • Yeah you're right about the indentation. I had it indented in my file but when I transferred it over it didn't paste quite right. In regards to the answer technically it is right because of how I explained my problem, but.... I don't to group a line that less than 10 numbers with another line – Kalimantan Aug 28 '16 at 01:31
  • This solution won't group short lines together. It starts the `for` loop fresh for each line. – Matthias Fripp Aug 28 '16 at 01:34
1

How about using [count] to count the occurrences of an item in the list?

list == [40, 20, 30, 50, 40, 40, 40, 40, 40, 40, 20]
for i in list:
    if list.count(i) > 10:
        # Do Stuff
Allen Butler
  • 337
  • 3
  • 12
  • Ive gone in that route, and thats what I was thinking but my biggest problem is line break every set of numbers, not only if they are greater than 10 – Kalimantan Aug 28 '16 at 00:54
  • Sorry I think I misunderstood the question the edits cleared some things up. I'll make an edit when I get home – Allen Butler Aug 28 '16 at 00:56