Convert CSV to txt and start new line every 10 values using Python
Question: I have a csv file with an array of values 324 rows and 495 columns. All the
values for each row and col are the same.

I need to have this array split up so that every 10 values is put in a new
row. So for each of the 324 rows, there will be 49 full columns with 10 values
and 1 column with 5 values (495 col / 10 values = 49 new rows with 10 values
and 1 new row with 5 values). Then go to the next row and so on for 324 rows.

The trouble i'm having is listed as follows:

  1. line.split(",") does not seem to be doing anything
  2. Everything after the line.split doesn't seem to do anything either
  3. i'm not sure my for newrow in range...is correct
  4. I haven't put in the write output to text file yet, i think it should be outFile.write(something goes here, not sure what)
  5. i put "\n" after print statement, but it just printed it out

I'm a beginner programmer.

Script:

    
    
    import string
    import sys
    
    # open csv file...in read mode
    inFile= open("CSVFile", 'r')
    outFile= open("TextFile.txt", 'w')
    
    
    for line in inFile:
        elmCellSize = line.split(",")
        for newrow in range(0, len(elmCellSize)):
            if (newrow/10) == int(newrow/10):
                print  elmCellSize[0:10]   
    
    outFile.close()
    inFile.close()
    


Answer: You should really be using the csv module, but I can give some advice anyway.

One problem you're having is that when you say `print elmCellSize[0:10]`, you
are always taking the first 10 elements, not the most recent 10 elements.
Depending on how you want to do this, you could keep a string to remember the
most recent 10 elements. I'll show an example below, after mentioning a few
things that you can fix with your code.

First note that `line.split(',')` returns a list. So your choice of variable
name `elmCellSize` is a little misleading. If you were to say `lineList =
line.split(',')` it might make more sense? Or if you were to say `lineSize =
len(line.split(','))` and use that?

Also (although I don't know anything about Python 2.x) I think `xrange` is a
function for Python 2.x which is more efficient than `range`, although it
works exactly the same way.

Instead of saying `if (newrow/10) == int(newrow/10)`, you can actually say `if
index % 10 == 0`, to check if index is a multiple of 10. `%` can be thought of
as 'remainder', so it will give the remainder of `newrow` when divided by
`10`. (Ex: 5 % 10 = 5; 17 % 10 = 7; 30 % 10 = 0)

Now instead of printing `[0:10]`, which will always print the first 10
elements, you want to print from the current index back 10 spaces. So you can
say `print lineList[index-10:index]` in order to print the most recent 10
elements.

In the end you'll have something like

    
    
    ...
    lineList = line.split(',') # Really, you should use csv reader
    # Open the file to write to
    with open('yourfile.ext', 'w') as f:
        # iterate through the line
        for index, value in enumerate(lineList):
            if index % 10 == 0 and index != 0:
                # Write the last 10 values to the file, separated by commas
                f.write(','.join(lineList[index-10:index]))
                # new line
                f.write('\n')
                # print
                print lineList[index-10:index]
    

I'm certainly not an expert, but I hope this helps!

