Python

Moderators: None (Apply to moderate this forum)
Number of threads: 473
Number of posts: 1172

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
reading from csv database Posted by nevle on 18 Mar 2003 at 12:13 AM
Hi,I'm new to python and programming and have been asked to write a script to read lines from a database for including in an html table.As the database runs to about 1000 line I need to split it into about 10-20 sections.How do I link a subject to a line number and then output all lines that occur until the next subject? Do I make the subject the key in a dictionary with the line number the value? I would greatly appreciate some input as I,m finding this very difficult without experienced advise.many thanks
nevle

Report
Re: reading from csv database Posted by infidel on 18 Mar 2003 at 7:31 AM
: Hi,I'm new to python and programming and have been asked to write a script to read lines from a database for including in an html table.As the database runs to about 1000 line I need to split it into about 10-20 sections.How do I link a subject to a line number and then output all lines that occur until the next subject? Do I make the subject the key in a dictionary with the line number the value? I would greatly appreciate some input as I,m finding this very difficult without experienced advise.many thanks

Could you describe the database structure? How is the data stored?


infidel

Report
Re: reading from csv database Posted by nevle on 24 Mar 2003 at 3:33 AM
: : Hi,I'm new to python and programming and have been asked to write a script to read lines from a database for including in an html table.As the database runs to about 1000 line I need to split it into about 10-20 sections.How do I link a subject to a line number and then output all lines that occur until the next subject? Do I make the subject the key in a dictionary with the line number the value? I would greatly appreciate some input as I,m finding this very difficult without experienced advise.many thanks
:
: Could you describe the database structure? How is the data stored?
:
:
: infidel
:
:Hi,Thanks for your reply,The data is a pricelist in excel,with columns for decsciption, price,tax, etc(7 items in all) with headings for each item.I can't figure out how to list all the lines from one heading to the next?It must be easy but I cannot see it!

nevle


Report
Re: reading from csv database Posted by infidel on 24 Mar 2003 at 8:45 AM
: :Hi,Thanks for your reply,The data is a pricelist in excel,with columns for decsciption, price,tax, etc(7 items in all) with headings for each item.I can't figure out how to list all the lines from one heading to the next?It must be easy but I cannot see it!

Sorry if I sound dense, but I'm having a hard time picturing what you're trying to do.


infidel

Report
Re: reading from csv database Posted by nevle on 24 Mar 2003 at 4:21 PM
: : :Hi,Thanks for your reply,The data is a pricelist in excel,with columns for decsciption, price,tax, etc(7 items in all) with headings for each item.I can't figure out how to list all the lines from one heading to the next?It must be easy but I cannot see it!
:
: Sorry if I sound dense, but I'm having a hard time picturing what you're trying to do.
:
:
:Hi Infidel,I'm obviously not describing my problem very well so I'll try from a different angle.How do I read specific rows from data in an Excel spreadsheet.I have tried variations of this:

file=open("/home/pricelist.csv","r")
line=(file.readlines())
file.close
rows=len(line)
list=[]
print rows
for count in range(1,rows):
myline=line[count]
splitline=re.split('","|\n',myline)
if splitline[0]=='CD/CDRW/DVD Drive':
print myline
list.append(count)
if splitline[0]=='Computer Cases':
print myline
list.append(count)
print list
Then I need to list all rows between these two headings.Am I doing this right?Sorry I've taken up so much of your time but thanks for your help


nevle


Report
Re: reading from csv database Posted by infidel on 25 Mar 2003 at 7:57 AM
: : : :Hi,Thanks for your reply,The data is a pricelist in excel,with columns for decsciption, price,tax, etc(7 items in all) with headings for each item.I can't figure out how to list all the lines from one heading to the next?It must be easy but I cannot see it!
: :
: : Sorry if I sound dense, but I'm having a hard time picturing what you're trying to do.
: :
: :
: :Hi Infidel,I'm obviously not describing my problem very well so I'll try from a different angle.How do I read specific rows from data in an Excel spreadsheet.I have tried variations of this:
:
: file=open("/home/pricelist.csv","r")
: line=(file.readlines())
: file.close
: rows=len(line)
: list=[]
: print rows
: for count in range(1,rows):
: myline=line[count]
: splitline=re.split('","|\n',myline)
: if splitline[0]=='CD/CDRW/DVD Drive':
: print myline
: list.append(count)
: if splitline[0]=='Computer Cases':
: print myline
: list.append(count)
: print list
: Then I need to list all rows between these two headings.Am I doing this right?Sorry I've taken up so much of your time but thanks for your help

Ah, ok. I didn't know what CSV files were so I made up a simple on in Excel to see what they look like. I'm with you now. Here is my script which reads the contents of a csv file, and prints out all the lines from the one that starts with 'CD/CDRW/DVD Drive' through the one that starts with 'Computer Cases':

lines = open("K:\\EXCEL\\Book1.csv","r").read().split("\n")
output = False
for line in lines:
    if line.find("CD/CDRW/DVD Drive") == 0:
        output = True
    elif line.find("Computer Cases") == 0:
        print line
        break
    if output: print line


Notice that you're making things more difficult than they need to be. Python is quite intelligent in its syntax. You can iterate through a list without ever using an index.


infidel

Report
Re: reading from csv database Posted by nevle on 26 Mar 2003 at 12:57 AM
: : : : :Hi,Thanks for your reply,The data is a pricelist in excel,with columns for decsciption, price,tax, etc(7 items in all) with headings for each item.I can't figure out how to list all the lines from one heading to the next?It must be easy but I cannot see it!
: : :
: : : Sorry if I sound dense, but I'm having a hard time picturing what you're trying to do.
: : :
: : :
: : :Hi Infidel,I'm obviously not describing my problem very well so I'll try from a different angle.How do I read specific rows from data in an Excel spreadsheet.I have tried variations of this:
: :
: : file=open("/home/pricelist.csv","r")
: : line=(file.readlines())
: : file.close
: : rows=len(line)
: : list=[]
: : print rows
: : for count in range(1,rows):
: : myline=line[count]
: : splitline=re.split('","|\n',myline)
: : if splitline[0]=='CD/CDRW/DVD Drive':
: : print myline
: : list.append(count)
: : if splitline[0]=='Computer Cases':
: : print myline
: : list.append(count)
: : print list
: : Then I need to list all rows between these two headings.Am I doing this right?Sorry I've taken up so much of your time but thanks for your help
:
: Ah, ok. I didn't know what CSV files were so I made up a simple on in Excel to see what they look like. I'm with you now. Here is my script which reads the contents of a csv file, and prints out all the lines from the one that starts with 'CD/CDRW/DVD Drive' through the one that starts with 'Computer Cases':
:
:
: lines = open("K:\\EXCEL\\Book1.csv","r").read().split("\n")
: output = False
: for line in lines:
:     if line.find("CD/CDRW/DVD Drive") == 0:
:         output = True
:     elif line.find("Computer Cases") == 0:
:         print line
:         break
:     if output: print line
: 

:
: Notice that you're making things more difficult than they need to be. Python is quite intelligent in its syntax. You can iterate through a list without ever using an index.
:
Hi Infidel,I want to thank you for your patience with me,I would not have thought of that solution.I will not bother you for a while as I try and expand what youhave shown me,I appreciate more than I can put into words people like you who are willing to spend their time helping others come to grips with what can be a daunting challenge.thanks
:
:

nevle





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.