PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, August 29, 2022

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

 August 29, 2022     csv, dictionary, list, python     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing