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
CodePudding user response:
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:
- Question: How do you split a list into evenly-sized chunks?;
- Documentation for
more_itertools.chunked
; - Itertools recipe: "Collect data into fixed-length chunks or blocks".
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']]