2017-03-08 9 views
0

ランダムユーザのCSVファイルを生成するのにrandomuser.me APIを使用しています。 プログラムは、CSVをHTMLテーブルに変換することになっています。 カラム[0, 3, 4]をスキップして、生成されたHTMLテーブルに表示しないようにしようとしています。 これを行うにはfor-loopif-conditionを使用しようとしています。 問題は、ループ内の変数columnintでなく、条件if (int(column) == 0 and int(column) == 3 and int(column) == 4)を作成できないことです。 解決方法を指摘することに非常に感謝します。CSVファイルをHTMLテーブルに変換するときにカラムをスキップ

import webbrowser 
import csv 
import sys 
import requests 

if len(sys.argv) < 2: 
    print("Usage: project.py <htmlFile>") 
    exit(0) 

url = 'https://randomuser.me/api/?results=2&format=csv&inc=name,picture' 
with requests.Session() as s: 
    download = s.get(url) 
    decodedContent = download.content.decode('utf-8') 
    csvFile = list(csv.reader(decodedContent.splitlines(), delimiter=',')) 


# Create the HTML file 
htmlFile = open(sys.argv[1], "w") 

# Fills up HTM code 
# Remove BOM from UTF-8 (wierd symbols in the beggining of table) 
htmlFile.write('<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><link rel="stylesheet" type="text/css" href="style.css"><head><title>Tabela</title></head><body><table>') 

# Read a single row from the CSV file 
for row in csvFile: 
    # Create a new row in the table 
    htmlFile.write('<tr>'); 
    # For each column 
    for column in row: 
     if (int(column) == 0 and int(column) == 3 and int(column) == 4): 
      continue 
     else: 
      htmlFile.write('<td>' + column + '</td>') 
    htmlFile.write('</tr>') 

htmlFile.write('</table></body></html>') 

webbrowser.open_new_tab('index.html') 

答えて

0

あなたはlistからエントリを削除するためにdelを使用することができます。だから、逆の順序で、ちょうどそれらを取り除く:

for c in [4,3,0]: 
    del row[c] 

リストから項目を削除すると、その項目の後にすべての番号を変更するので、それは逆の順序を使用することが重要です。だから常に最後から最初に行く。

0

は、私はあなたが使用しているRandomUserのAPIに精通していないんだけど、論理的に、私は次の文は真であることが可能であることを期待していない:

int(column) == 0 and int(column) == 3 and int(column) == 4 

でき欄等しいすべてのそれらの値同時に?おそらくないだろう。私は、列がいずれかの列0 または

+0

ファイルは必要ありません、削除、言ったように。または文でなければなりません。それでもまだ問題は解決していません。 – Daniel

0

4.あなたの助けをありがとう3 またはコラムであろうと予想されます。

オースティンヘイスティングスは、私はCSVの列ははい、あなたは正しいです

for row in csvFile: 
    for column in [4,3,0]: 
     del row[column] 
関連する問題