简体   繁体   English

将CSV文件转换为HTML表时跳过列

[英]Skip columns while converting CSV file to HTML table

I am using randomuser.me API to generate random users CSV file. 我正在使用randomuser.me API生成随机用户CSV文件。 Program is supposed to convert the CSV to HTML table. 程序应该将CSV转换为HTML表。 I am trying to skip column [0, 3, 4] to not be shown in generated HTML Table. 我试图跳过列[0, 3, 4]使其不显示在生成的HTML表中。 To do this I am trying to use for-loop and if-condition . 为此,我尝试使用for-loopif-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) . 问题是循环中的变量column不是int并且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 . 您可以使用dellist删除条目。 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: 我不熟悉您使用的RandomUser API,但是从逻辑上讲,我不希望以下语句为真:

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. 我期望列要么是第0列第3列第4栏。

Thank you for your help. 谢谢您的帮助。

As Austin Hastings said, I deleted not needed columns in CSV file 正如Austin Hastings所说,我删除了CSV文件中不需要的列

for row in csvFile:
    for column in [4,3,0]:
        del row[column]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM