Python Forum
Read data from a CSV file in S3 bucket and store it in a dictionary in python
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Read data from a CSV file in S3 bucket and store it in a dictionary in python
#4
(May-14-2020, 11:52 AM)Rupini Wrote: is there other ways for me to achieve the same without CSV module?
Yes you can write a own csv.DictReader implementation.
Also if you look at csv module at top there is a link to Source code: Lib/csv.py .
So there can look at how they have written it,the important part start at line 119.

Here is start with some good hints,as this is homework there missing a little part.
lst = []
with open("log.csv") as f:
    header = next(f)
    header = header.strip().split(',')
    for row in f:
        row = row.strip().split(',')
        print(row)
Look at what have now.
['xxx', 'uk', 'france']
['yyyy', 'norway', 'finland']
['zzzz', 'denmark', 'canada']

>>> header
['name', 'origin', 'dest']

>>> row
['zzzz', 'denmark', 'canada']

# Now can test line 119
>>> dict(zip(header, row))
{'name': 'zzzz', 'origin': 'denmark', 'dest': 'canada'}
Reply


Messages In This Thread
RE: Read data from a CSV file in S3 bucket and store it in a dictionary in python - by snippsat - May-15-2020, 04:57 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  dictionary output to text file (beginner) Delg_Dankil 2 1,204 Jul-12-2023, 11:45 AM
Last Post: deanhystad
  Using dictionary to find the most sent emails from a file siliusu 6 7,607 Apr-22-2021, 06:07 PM
Last Post: siliusu
  Updating dictionary in another py file tommy_voet 1 4,914 Mar-28-2021, 07:25 PM
Last Post: buran
  Making a dictionary from a file instyabam 0 1,517 Oct-27-2020, 11:59 AM
Last Post: instyabam
  how can i create a dictionary of dictionaries from a file Astone 2 2,275 Oct-26-2020, 02:40 PM
Last Post: DeaD_EyE
  Convert all actions through functions, fill the dictionary from a file Astone 3 2,452 Oct-26-2020, 09:11 AM
Last Post: DeaD_EyE
  Can we store value in file if we open file in read mode? prasanthbab1234 3 2,583 Sep-26-2020, 12:10 PM
Last Post: ibreeden
  [split] how to read a specific row in CSV file ? laxmipython 2 8,891 May-22-2020, 12:19 PM
Last Post: Larz60+
  Read text file, process data and print specific output Happythankyoumoreplease 3 2,932 Feb-20-2020, 12:19 PM
Last Post: jefsummers
  Store data in array kasper1903 4 3,131 Oct-04-2019, 12:43 PM
Last Post: kasper1903

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020