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
8
  • 1
    Hello! Nice code. And what is your question? Commented Sep 16, 2021 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. Commented Sep 16, 2021 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'] Commented Sep 16, 2021 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 <module> t.append(rows[line][i]+rows[line][j]+rows[line][k]) IndexError: list index out of range Commented Sep 16, 2021 at 12:40
  • i get this solution but for one row and after that be out of range Commented Sep 16, 2021 at 12:41

1 Answer 1

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']]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.