Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

reading from csv database

nevlenevle Posts: 12Member
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

Comments

  • infidelinfidel Posts: 2,900Member
    : 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?


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

  • nevlenevle Posts: 12Member
    : : 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?
    :
    :
    : [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]
    :
    :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


  • infidelinfidel Posts: 2,900Member
    : :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.


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

  • nevlenevle Posts: 12Member
    : : :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('","|
    ',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


  • infidelinfidel Posts: 2,900Member
    : : : :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('","|
    ',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':

    [code]
    lines = open("K:\EXCEL\Book1.csv","r").read().split("
    ")
    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
    [/code]

    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.


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

  • nevlenevle Posts: 12Member
    : : : : :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('","|
    ',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':
    :
    : [code]
    : lines = open("K:\EXCEL\Book1.csv","r").read().split("
    ")
    : 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
    : [/code]
    :
    : 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


Sign In or Register to comment.