I would appreciate it if somebody could tell me where I went wrong in
the following snipet:
It would help if you gave a sample of the input data (three lines
worth, say) AND an example of what the final output should be from those
three lines.
for col in line:
p[:0].append(str(col))
As has been pointed out, as soon as you used the [:0], you created a
local/temporary EMPTY slice of the original P, and you are appending one
column's value to this temporary, which is then thrown away.
import csv
-=-=-=-=-=-=-=- PROGRAM
p = []
fin = open("firearms.csv", "r")
reader = csv.reader(fin, dialect="excel", quotechar='"', delimiter=",")
for line in [reader.next() for i in range(7)]:
p.append(line)
fin.close()
print p
-=-=-=-=-=-=-=- OUTPUT
[['Category', 'Model', 'Caliber', 'Serial #', 'Description',
'Accessories'], ['Air', 'Daisy 717 Pistol', '.177 pellet', '', '', ''],
['Air', 'Daisy/NRA PowerLine 953 Rifle', '.177 pellet', 'n/a',
'Micrometer Peep, "Globe" front (missing alternate inserts', 'Shooting
sling'], ['Air', 'RWS Diana Model 54 "Air King" Rifle', '.22 pellet',
'4022395', 'Hunting grade - >900fps', '2-7x BSA AOL scope'], ['Air',
'Gamo/NRA', '0.177', '', 'Hunting grade - ~1000fps; NRA markings on
barrel, stock', '4x (BSA?) AOL scope, NRA badge'], ['Air',
'Walther/Crossman CP99 Pistol', '.177 pellet', '', 'CO2, repeater
(currently magazine jams trigger/safety)', ''], ['Percussion', '? New
Orleans Ace boot-pistol', '.36 lead', '', '', '']]
-=-=-=-=-=-=-=- INPUT (just first seven lines)
Category,Model,Caliber,Serial #,Description,Accessories
Air,Daisy 717 Pistol,.177 pellet,,,
Air,Daisy/NRA PowerLine 953 Rifle,.177 pellet,n/a,"Micrometer Peep,
""Globe"" front (missing alternate inserts",Shooting sling
Air,"RWS Diana Model 54 ""Air King"" Rifle",.22 pellet,4022395,Hunting
grade - >900fps,2-7x BSA AOL scope
Air,Gamo/NRA,0.177,,"Hunting grade - ~1000fps; NRA markings on barrel,
stock","4x (BSA?) AOL scope, NRA badge"
Air,Walther/Crossman CP99 Pistol,.177 pellet,,"CO2, repeater (currently
magazine jams trigger/safety)",
Percussion,? New Orleans Ace boot-pistol,.36 lead,,,
But your explanations are unclear... Maybe you wanted the first
sublist to be all the first column, etc.
-=-=-=-=-=-=-=- PROGRAM
import csv
p = None
fin = open("firearms.csv", "r")
reader = csv.reader(fin, dialect="excel", quotechar='"', delimiter=",")
for line in [reader.next() for i in range(7)]:
if not p:
p = [[] for j in range(len(line))]
for c in range(len(line)):
p[c].append(line[c])
fin.close()
print p
-=-=-=-=-=-=-=- OUTPUT (same input)
[['Category', 'Air', 'Air', 'Air', 'Air', 'Air', 'Percussion'],
['Model', 'Daisy 717 Pistol', 'Daisy/NRA PowerLine 953 Rifle', 'RWS
Diana Model 54 "Air King" Rifle', 'Gamo/NRA', 'Walther/Crossman CP99
Pistol', '? New Orleans Ace boot-pistol'], ['Caliber', '.177 pellet',
'.177 pellet', '.22 pellet', '0.177', '.177 pellet', '.36 lead'],
['Serial #', '', 'n/a', '4022395', '', '', ''], ['Description', '',
'Micrometer Peep, "Globe" front (missing alternate inserts', 'Hunting
grade - >900fps', 'Hunting grade - ~1000fps; NRA markings on barrel,
stock', 'CO2, repeater (currently magazine jams trigger/safety)', ''],
['Accessories', '', 'Shooting sling', '2-7x BSA AOL scope', '4x (BSA?)
AOL scope, NRA badge', '', '']]
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/