Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to convert CSV file contents to HTML output in python

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Python how to convert the contents of CSV files into HTML output, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

First, take a look at the person_info.csv file on my desktop, which is as follows:

Next, create a new python file named py3_csv2html.py, and write the operation code in this file:

Import csv#### displays the name columns in the csv file to the html # defines the html output variable html_output =''# defines the list list storage name names = [] encode = 'UTF-8 sig'with open (' person_info.csv','r',encoding=encode) as csv_file: csv_data = csv.reader (csv_file) # according to the format of the data shown above We do not need # header and the first line of abnormal data # use next () skip over # next () method and learn to say # you can see the screenshot below to understand next (csv_data) next (csv_data) for line in csv_data: # add the name in the file to names # here fstring is used instead of string's format names.append (f "{line [0]}")

Html_output + = f "

There are a total of {len (names)} individuals in the file.

"html_output + ="\ n "for name in names: html_output + = f"\ n\ t {name} "html_output + ="\ n "print (html_output) # * the above is implemented using csv's reader method * # you can achieve the above function according to the second DictReader () # method of csv in the previous article, so I won't demonstrate it here.

Parse the data in person_info.csv and get the following figure:

Here we don't need the header and the first row of bad data, so we use next () twice.

The result diagram of the above code:

After reading the above, have you mastered how to convert the contents of CSV files into HTML output in python? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report