0

i have CSV file as the following:

aa , a1, 1, bb, b1, 11, cc, c1, 111
aa2 , a2, 2, bb2, b2, 22, cc2, c2, 222
aa3 , a3, 3, bb3, b3, 33, cc3, c3, 333

and I need to take the first three content and store it in array like: aa , a1, 1, bb, b1, 11, cc, c1, 111 will become into ['aa a1 1', 'bb b1 11', 'cc c1 111']

and this my code:

import csv

with open('Test.csv') as csv_file:
    csv_reader = csv.reader(csv_file)
    rows = list(csv_reader)
    lines= len(list(rows))

    for line in range(lines):
        i=0
        j=1
        k=2

        for l in range(len(list(rows[line]))):
            t=[]
            t.append(rows[line][i]+rows[line][j]+rows[line][k])
            print(t[0:])
            i+=3
            j+=3
            k+=3
  • 1
    Hello! Nice code. And what is your question? – Stef Sep 16 '21 at 12:35
  • Note the code would probably be slightly more readable if you write `for row in lines:` rather than `for line in range(lines):`, and then replace every occurrence of `rows[line]` by simply `row`. – Stef Sep 16 '21 at 12:37
  • i want to get each 3 column like aa, a , 1 and store it in as one column until i get this ['aa a1 1', 'bb b1 11', 'cc c1 111'] – Kitaro Asda Sep 16 '21 at 12:38
  • with my code i get this error ['aa a1 1'] [' bb b1 11'] [' cc c1 111'] Traceback (most recent call last): File "C:\Users\kitar\Desktop\oop.py", line 18, in t.append(rows[line][i]+rows[line][j]+rows[line][k]) IndexError: list index out of range – Kitaro Asda Sep 16 '21 at 12:40
  • i get this solution but for one row and after that be out of range – Kitaro Asda Sep 16 '21 at 12:41
  • What do you want in the end? More exactly what do want to do with lines following the first one? – Serge Ballesta Sep 16 '21 at 12:59
  • i want to sorting it sort 'aa a 1' in the first and 'bb...' second and so on – Kitaro Asda Sep 16 '21 at 13:02
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 23 '21 at 14:24

1 Answers1

1

With for-loops, it is usually preferable to iterate on the items directly, using for instance for row in rows:, rather than on the indices, using for instance for line in range(lines):.

Also, note that every time you call list, you are building a new list. This can be wasteful. For instance, lines = len(list(rows)) builds an unnecessary copy of the list rows. You could have simply written nb_lines = len(rows).

Since you want to split your list into chunks of 3, you might be interested in reading these:

import csv

t = []
with open('Test.csv') as csv_file:
    for row in csv.reader(csv_file):
        t.append([' '.join(row[i:i+3]) for i in range(3)])
print(t)
# [['aa   a1  1', ' a1  1  bb', ' 1  bb  b1'],
#  ['aa2   a2  2', ' a2  2  bb2', ' 2  bb2  b2'],
#  ['aa3   a3  3', ' a3  3  bb3', ' 3  bb3  b3']]

We can get rid of the bad-looking extra spaces, using str.strip:

import csv

t = []
with open('Test.csv') as csv_file:
    for row in csv.reader(csv_file):
        t.append([' '.join([word.strip() for word in row[i:i+3]]) for i in range(3)])
print(t)
# [['aa a1 1', 'a1 1 bb', '1 bb b1'],
#  ['aa2 a2 2', 'a2 2 bb2', '2 bb2 b2'],
#  ['aa3 a3 3', 'a3 3 bb3', '3 bb3 b3']]
Stef
  • 13,242
  • 2
  • 17
  • 28