Need help writing a program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • on3word
    New Member
    • May 2007
    • 2

    Need help writing a program

    I need help with a program that reads lines from a CSV input file and generates an output file in tabular format. So the first line in the input file should have the header infromation for the table and all the other lines are data lines. After I need to create a variant that gives the same output file and generates an HTML
    file. So it's supposed to have an HTML table essentially, with a row
    for each data line.

    So an example of the CSV file would look like this:
    Country, Code, Capital, Language, Currency
    Afghanistan,AF, Kabul,Afghani,P ashto
    Albania,AL,Tira na,Lek,Albanian
    Algeria,DZ,Algi ers,Dinar,Arabi c
    Andorra,AD,And. La Vella,French Franc,Catalan
    Angola,AO,Luand a,New Irwanza,Portugu ese
    Anguilla,AI,,Ea st Caribbean Dollar,

    And the output should come like this:
    COUNTRY CODE CAPITAL LANGUAGE CURRENCY
    Afghanistan AF Kabul Afghani Pashto
    Albania AL Tirana Lek Albanian
    Algeria DZ Algiers Dinar Arabic
    Andorra AD And. La Vella French Franc Catalan
    Angola AO Luanda New Irwanza Portuguese
    Anguilla AI East Caribbean Dollar

    Any help please?
  • dshimer
    Recognized Expert New Member
    • Dec 2006
    • 136

    #2
    IMHO see
    Join 420,000+ software developers and IT professionals

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      There are several threads on this forum dealing with reading and writing files, some regarding csv and tab delimited data. I am sure we can help you, but you must show us some effort toward solving your assignment.

      Comment

      • ghostdog74
        Recognized Expert Contributor
        • Apr 2006
        • 511

        #4
        Originally posted by on3word
        I need help with a program that reads lines from a CSV input file and generates an output file in tabular format. So the first line in the input file should have the header infromation for the table and all the other lines are data lines. After I need to create a variant that gives the same output file and generates an HTML
        file. So it's supposed to have an HTML table essentially, with a row
        for each data line.

        So an example of the CSV file would look like this:
        Country, Code, Capital, Language, Currency
        Afghanistan,AF, Kabul,Afghani,P ashto
        Albania,AL,Tira na,Lek,Albanian
        Algeria,DZ,Algi ers,Dinar,Arabi c
        Andorra,AD,And. La Vella,French Franc,Catalan
        Angola,AO,Luand a,New Irwanza,Portugu ese
        Anguilla,AI,,Ea st Caribbean Dollar,

        And the output should come like this:
        COUNTRY CODE CAPITAL LANGUAGE CURRENCY
        Afghanistan AF Kabul Afghani Pashto
        Albania AL Tirana Lek Albanian
        Algeria DZ Algiers Dinar Arabic
        Andorra AD And. La Vella French Franc Catalan
        Angola AO Luanda New Irwanza Portuguese
        Anguilla AI East Caribbean Dollar

        Any help please?
        just doing the tabular part, i see the output, its just removing the ",".
        so you can just read your file line by line, then use the replace() method to replace the ",".
        Code:
        eg
        for line in open("yourcsv"):
            line = line.replace("," , "\t")
            print line

        Comment

        • on3word
          New Member
          • May 2007
          • 2

          #5
          Thanks to everyone that replied, and thanks in advance to those who will.

          I know for the first main part of the program I need to get/set inputs and the open in/out files for reading/writing. And then get rid off \n at end, then split on ',' --> header_list:

          I'm mostly having trouble with the syntax though and getting that work in IDLE.

          Comment

          Working...