Monday, August 29, 2022

[FIXED] Why my List show a string separate the characters and showing it as list

Issue

My current csv file :

  'Date','Category','Ability' 
  '21,14,5','Sparrow','Air,land' 
  '4,5,6','Eagle','Air,Land'
  '21,14,5','Penguin','water,land'

my code:

Living_beings=[]
with open(users_read,'r') as f:
  reader=DictReader(f)
  for row in reader:
    if date.today().day in row['Date']:
         Living_beings+=row['Category']
  print(Living_beings)

Output ; ['S','p','a','r','r','o','w','P','e','n','g','u','i','n']

Expected output: [Sparrow, penguin]

I am not sure why it was split up...Any ideas on this.


Solution

Try Living_beings.append(row['Category']) instead.

My suspicion is the original code is treating row['Category'] as a list of individual characters so it can combine the lists.



Answered By - davidli
Answer Checked By - Clifford M. (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.