[Tutor] exporting lists into CSV issue.

mhysnm1964 at gmail.com mhysnm1964 at gmail.com
Tue Jan 22 05:16:20 EST 2019


All,

 

I have addressed the issue. When I open the file, I am using newline='' as
one of the parameters. 

 

with open ('books-list.csv', 'w', newline='', encode='utf-8') as wf:

 

I added the UTF-8 as I discovered after sending my original email I was not
importing all the books. I discovered I was getting an utf-8 error due to
some of the content. Now I have all my books in a spreadsheet. Stage 2, is
to cross reference all this information against my audible library which I
will have to extract.

 

All this is to learn the different methods of extracting data.

 

 

From: mhysnm1964 at gmail.com <mhysnm1964 at gmail.com> 
Sent: Tuesday, 22 January 2019 8:26 PM
To: tutor at python.org
Subject: exporting lists into CSV issue.

 

All,

 

Thanks in advance for any assistance. This is a follow up msg from my iTunes
import issue with the xml. I have managed to successfully import the data
from the XML with Peter's and Alan's help. I have moved the information into
a list and only extracting the data I want. Now I am trying to export to a
CSV file. There is no syntax or logical errors I can see. The information is
being exported to the CSV. But when I bring it into Excel. I am getting a
blank row between each row. The exported data looks like this:

 

Name,Artist,Kind,Total Time,Year,Date Modified,Date Added,Bit Rate,Sample
Rate,Comments,Location "In Her Sights: A Montgomery Justice Novel, Book 1
(Unabridged)",Robin Perini,Audible file,25574318,2012,2015-12-12
23:48:18,2015-12-12 23:48:20,64,22050,"Jasmine 'Jazz' Parker, Jefferson
County SWAT's only female sniper, can thread the eye of a needle with a
bullet. But she carries with her a secret from her
past....","file:///Volumes/Itunes/iTunes/iTunes%20Media/Audiobooks/Robin%20P
erini/In%20Her%20Sights_%20A%20Montgomery%20Justice%20Novel,%20Book%201%20(U
nabridged).aax" "Behind the Lies: A Montgomery Justice Novel, Book 2
(Unabridged)",Robin Perini,Audible file,35142797,2013,2015-12-12
23:53:33,2015-12-12 23:53:33,64,22050,"Of the six Montgomery brothers, Zach
has always walked on the wild side. He rocketed to fame playing a hero in a
movie, but off screen he's living in the shadows. Zach's dark secret: He
leads a double life as a CIA
operative....",file:///Volumes/Itunes/iTunes/iTunes%20Media/Audiobooks/Robin
%20Perini/Behind%20the%20Lies_%20A%20Montgomery%20Justice%20Novel,%20Book%20
2%20(Unabridged).aax

 

I would have expected a new line between each book, but there is a space.
The code looks like this:

 

import plistlib, pprint, csv 

 

with open("library.xml", "rb") as f:

    data = plistlib.load(f)

 

books =[['Name', 'Artist', 'Kind', 'Total Time', 'Year', 'Date Modified',
'Date Added', 'Bit Rate', 'Sample Rate', 'Comments', 'Location']]

 

for book in list(data['Tracks'].values()):

    tmp = []

    if not 'Audible file' in book['Kind']:

        break # skip book

    for key in books[0]:

        if key in book:

            tmp.append(book[key])

        else:

            tmp.append("")

    books.append(tmp)

 

with open ('books-list.csv', 'w') as wf:

    writer = csv.writer(wf)

    writer.writerows(books)

wf.close()

 

When I debug the above. The output of books look fine to me. For example:

 

(Pdb)
['Name', 'Artist', 'Kind', 'Total Time', 'Year', 'Date Modified', 'Date
Added', 'Bit Rate', 'Sample Rate', 'Comments', '
Location']
(Pdb)
(Pdb) books[0] 
]
0
['In Her Sights: A Montgomery Justice Novel, Book 1 (Unabridged)', 'Robin
Perini', 'Audible file', 25574318, 2012, datet
ime.datetime(2015, 12, 12, 23, 48, 18), datetime.datetime(2015, 12, 12, 23,
48, 20), 64, 22050, "Jasmine 'Jazz' Parker,
Jefferson County SWAT's only female sniper, can thread the eye of a needle
with a bullet. But she carries with her a sec
ret from her past....",
'file:///Volumes/Itunes/iTunes/iTunes%20Media/Audiobooks/Robin%20Perini/In%2
0Her%20Sights_%20A%2
0Montgomery%20Justice%20Novel,%20Book%201%20(Unabridged).aax']
(Pdb)
(Pdb) books[1] 
]
1
['Behind the Lies: A Montgomery Justice Novel, Book 2 (Unabridged)', 'Robin
Perini', 'Audible file', 35142797, 2013, dat
etime.datetime(2015, 12, 12, 23, 53, 33), datetime.datetime(2015, 12, 12,
23, 53, 33), 64, 22050, 'Of the six Montgomery
brothers, Zach has always walked on the wild side. He rocketed to fame
playing a hero in a movie, but off screen he's l
iving in the shadows. Zach's dark secret: He leads a double life as a CIA
operative....', 'file:///Volumes/Itunes/iTunes
/iTunes%20Media/Audiobooks/Robin%20Perini/Behind%20the%20Lies_%20A%20Montgom
ery%20Justice%20Novel,%20Book%202%20(Unabrid
ged).aax']
(Pdb)

 

Any ideas why this could be occurring? 

 

Regards

Sean 



More information about the Tutor mailing list