Python Forum
convert text file data to HTML table in python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
convert text file data to HTML table in python
#1
Hi All,


i am using python 2.7.

i want convert  the text data to  HTML  table.

Below is my data ,please can you help me to convert the  below data  to HTML table?

No Name
10 Administration
20 Marketing
30 Purchasing
40 Human Resources
50 Shipping
60 IT
70 Public Relations
80 Sales
90 Executive
100 Finance
110 Accounting
120 Treasury
130 Corporate Tax
140 Control And Credit
150 Shareholder Services
160 Benefits
170 Manufacturing
180 Construction
190 Contracting
200 Operations
210 IT Support

Thanks
TR
Reply
#2
what have you tried? please post your code in code tags, full traceback in error tags (if you get any) and ask specific questions.
We are glad to help, but we are not doing our work for you.
Reply
#3
Hi ,

I am using below code 

contents = open("D:\Projects\Automations\dbresult.txt","r")
with open("D:\Projects\Automations\dbresult.html", "w") as e:
    e.write("<table>\n")   
    for lines in contents.readlines():
        e.write("<tr><td>%s</td><td>%s</td></tr>\n"%lines.split())
    e.write("</table>\n")

Getting following  TypeError: not enough arguments for format string error

Thanks
Reply
#4
you are using old-style formatting and according to docs (https://docs.python.org/2/library/stdtyp...formatting):
Quote:If format requires a single argument, values may be a single non-tuple object. [5] Otherwise, values must be a tuple with exactly the number of items specified by the format string, or a single mapping object (for example, a dictionary).

in your case lines.split() returns list, not tuple and you get the above error. You need to convert lines.split() to tuple if you want to use old-style string formatting. Much better would be to use new-style - i.e. .format() method.

Also note that you have names with space in it, e.g. IT support
Just using split will yield unexpected result/rise error.

I suggest you try to amend your code and if you not succeed I will give additional hints.
Reply
#5
Quote: e.write("<tr><td>%s</td><td>%s</td></tr>\n"%lines.split())
Error:
[b]TypeError: not enough arguments for format string[/b] error

Seems like that's all the help you need :)
Your string has two format specifiers, but your lines.split() has more (or less) than two items.
Reply
#6
(Dec-29-2017, 04:28 PM)nilamo Wrote: Your string has two format specifiers, but your lines.split() has more (or less) than two items.

no, it rise the error on the first line because it expects tuple, single argument or dict.
later on when there are more than 2 elements (given you supply tuple) it rise different error - TypeError: not all arguments converted during string formatting
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [SOLVED] Correct way to convert file from cp-1252 to utf-8? Winfried 8 880 Feb-29-2024, 12:30 AM
Last Post: Winfried
  Replace a text/word in docx file using Python Devan 4 3,452 Oct-17-2023, 06:03 PM
Last Post: Devan
  Going through HTML table with selenium emont 3 815 Sep-30-2023, 02:13 AM
Last Post: emont
Thumbs Up Convert word into pdf and copy table to outlook body in a prescribed format email2kmahe 1 759 Sep-22-2023, 02:33 PM
Last Post: carecavoador
  Using pyodbc&pandas to load a Table data to df tester_V 3 827 Sep-09-2023, 08:55 PM
Last Post: tester_V
  Need to replace a string with a file (HTML file) tester_V 1 775 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  Color a table cell based on specific text Creepy 11 2,008 Jul-27-2023, 02:48 PM
Last Post: deanhystad
  Convert File to Data URL michaelnicol 3 1,177 Jul-08-2023, 11:35 AM
Last Post: DeaD_EyE
  save values permanently in python (perhaps not in a text file)? flash77 8 1,244 Jul-07-2023, 05:44 PM
Last Post: flash77
  Python Script to convert Json to CSV file chvsnarayana 8 2,535 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020