-1
辞書にcsvファイルを読み込んで、pythonで辞書から特定の値を出力する方法を知りたい。 CSV file example。私は選択された行をつかんでその人の名前を印刷したいと思います。私はこれで新しく、いくつかのループを試しましたが、ループは最後の人に向かいます。Python:CSVから読み込まれた辞書リストから特定の値を出力する
import csv
with open('data.csv') as csvFile:
readCSV = list(csv.DictReader(csvFile))
for row in readCSV:
person1 = row['firstname'] + ' ' + row['lastname']
with open('nametags8gen.html', 'w+') as myWriteFile:
myWriteFile.write('<!DOCTYPE html> \n'
'<html>\n'
'<head>\n'
'<title>natetag8</title>\n'
'<link href="styles/nametags8.css" type="text/css" rel="stylesheet" />\n'
'</head>\n'
'<body>\n'
'<header>\n'
'</header>\n'
'<main class="mainContainer">\n'
'<div class"textBoxContainer">\n'
'<div class="textContainer">\n'
'<span class="font22">' + person1 +'</span>\n'
'<span class="font12">Smith</span>\n'
'<span class="font14">Web Developer</span>\n'
'<span class="font12">Regis University</span>\n'
'<span class="font12">Denver, CO</span>\n'
'</div>\n')
これは、私が必要とする特定の人ではなく、最後の人をループして取得します。
「書き込み」セクションは人物情報を配置する場所です。具体的には、自分のHTMLテンプレートの「person1」という場所に、私がCSVファイルから取った名字がどこにあるのかが記載されています。私は、ファーストネームやその住所などの情報を引き出すために、特定の人/行にループを止める方法を知らない。正確にあなたが達成しようとしているどのようなコードなし
そのまだ不明であるがimport csv
l = []
with open('inputfile.csv') as f:
reader = csv.DictReader(f)
for row in reader:
l.append(row)
# If you need say for example the firstname
for val in l:
print (l['firstname'])
:
便利な回答が必要な場合は、使用しようとしているコードを追加してください。 –
Repost of http://stackoverflow.com/questions/38748931/looping-through-dictionary-built-from-csv-and-write-certain-key-values-to-file? – handle