sudoedit,

Ok guru's.

I'm working on a very simple csv reader and I can't get the value from the first column.

I've got a csv file like this:
Last Name,First Name
Smith,Joe

and then a short python script like this:
import csv

with open("test.csv",'r', encoding='utf-8-sig') as f:
reader = csv.DictReader(f)
for row in reader:
print(row['First Name'], row['Last name'])
print(row)

if I remove row['Last Name'] from the print statement I get the first names.

kleaders,
@kleaders@fosstodon.org avatar

@sudoedit if you don't have any constraints around using external libraries, pandas and Polars both have really great CSV capabilities. A bit overkill though if you don't plan on doing a lot of data manipulation.

Is it throwing an error? Or just returning None? It looks like you may need to capitalize the n in Last Name as well.

sudoedit,

@kleaders I realized the n wasn't capitalized about 10 seconds after I posted that.... and I'm embarrassed to admit how long I was looking at this before I saw it...

Thanks

meejah,
@meejah@mastodon.social avatar

@sudoedit @kleaders Partly why http://www.sscce.org/ is often successful, or the "Rubber Duck" concept: explaining or asking for help often leads to the answer directly :)

  • All
  • Subscribed
  • Moderated
  • Favorites
  • python
  • tacticalgear
  • DreamBathrooms
  • everett
  • magazineikmin
  • osvaldo12
  • InstantRegret
  • Youngstown
  • mdbf
  • slotface
  • rosin
  • thenastyranch
  • kavyap
  • khanakhh
  • cisconetworking
  • JUstTest
  • Durango
  • GTA5RPClips
  • ethstaker
  • provamag3
  • ngwrru68w68
  • normalnudes
  • cubers
  • tester
  • modclub
  • megavids
  • anitta
  • Leos
  • lostlight
  • All magazines