Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Manipulating Binary Data
#11
Quote:My dictionary contains 1 key ('AA08') and then a list of all the data [data1, data2, data3 etc..] corresponding to that 1 key.

This way and it will keep the structure in and out of csv.
Can read it in with DictReader.
import csv

d = {'AA08': ['430022', '410234', '1111'], 'AB81': ['130138', '22222', '4444']}
with open("out.csv", "w") as out_file:   
    writer = csv.writer(out_file)
    writer.writerow(d.keys())
    writer.writerows(zip(*d.values()))
Output:
AA08, AB81 430022, 130138 410234, 22222 1111, 4444
Read it back.
>>> import csv
... with open('out.csv') as f:
...     reader = csv.DictReader(f)
...     for row in reader:
...         print(row['AA08'], row['AB81'])
430022 130138
410234 22222
1111 4444
Using Python 36 it will also return a OrderedDict.
As dictionary are ordered in 36.
>>> type(row)
<class 'collections.OrderedDict'>
>>> row
OrderedDict([('AA08', '1111'), ('AB81', '4444')])
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to convert binary data into text? ZYSIA 3 2,586 Jul-16-2021, 04:18 PM
Last Post: deanhystad
  Binary data to Image convert Nuwan16 1 5,562 Aug-24-2020, 06:03 AM
Last Post: millpond
  Manipulating data from a CSV EvanS1 5 2,679 Jun-12-2020, 05:59 PM
Last Post: perfringo
  manipulating two lists rancans 8 3,112 Apr-16-2020, 06:00 PM
Last Post: deanhystad
  Manipulating index value, what is wrong with this code? Emun 1 1,728 Feb-05-2020, 07:18 AM
Last Post: perfringo
  Manipulating the filename of an output script mckinneycm 4 11,828 Jan-15-2020, 07:29 PM
Last Post: mckinneycm
  hex file to binary or pcap to binary baran01 1 5,630 Dec-11-2019, 10:19 PM
Last Post: Larz60+
  Manipulating Excel with Python. Spacely 2 3,587 Jun-25-2019, 01:57 AM
Last Post: Dequanharrison
  How to Read Binary Data pyth0nus3r 1 2,178 Jun-09-2019, 08:58 PM
Last Post: DeaD_EyE
  Parse Binary Data File and convert Epoch Time drdevereaux 1 3,122 May-16-2019, 01:56 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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