By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,414 Members | 1,057 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,414 IT Pros & Developers. It's quick & easy.

Need help writing a program

P: 2
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,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,

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?
May 17 '07 #1
Share this Question
Share on Google+
4 Replies


dshimer
Expert 100+
P: 136

bvdet
Expert Mod 2.5K+
P: 2,851
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.
May 17 '07 #3

Expert 100+
P: 511
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,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,

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 ",".
Expand|Select|Wrap|Line Numbers
  1. eg
  2. for line in open("yourcsv"):
  3.     line = line.replace("," , "\t")
  4.     print line
  5.  
  6.  
May 17 '07 #4

P: 2
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.
May 17 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.