I am using randomuser.me API
to generate random users CSV file. Program is supposed to convert the CSV to HTML table. I am trying to skip column [0, 3, 4]
to not be shown in generated HTML Table. To do this I am trying to use for-loop
and if-condition
. The problem is that the variable column
in the loop is not an int
and I cannot create condition if (int(column) == 0 and int(column) == 3 and int(column) == 4)
. I would be very grateful for pointing the solution how to solve it.
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')
You can use del
to remove entries from a list
. So, in reverse order, just get rid of them:
for c in [4,3,0]:
del row[c]
It's important to use reverse order, because deleting items from a list changes the numbering of everything after that item. So always go from the last to the first.
I'm not familiar with the RandomUser API you are using, but logically, I would not expect it to be possible for the following statement to be true:
int(column) == 0 and int(column) == 3 and int(column) == 4
Can the column equal all those values at the same time? Seems unlikely. I would expect that the column would either be column 0 or column 3 or column 4.
Thank you for your help.
As Austin Hastings said, I deleted not needed columns in CSV file
for row in csvFile:
for column in [4,3,0]:
del row[column]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.