我使用randomuser.me API
生成隨機用戶CSV文件。 程序應該將CSV轉換爲HTML表格。 我試圖跳過列[0, 3, 4]
不會顯示在生成的HTML表中。 爲此,我嘗試使用for-loop
和if-condition
。 問題是循環中的變量column
不是int
,我無法創建條件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')
是的,你是對的。應該是或聲明。它仍然沒有解決問題。 – Daniel