I was hoping some one could help me out, I need to search a column in .csv and.las files, return the maximum and the corresponding value in another row.
Thank you

Recommended Answers

All 7 Replies

Do a litle search on the forum, theres a lot of CSV search and modify examples.

Write a tryout, and post the results, with input files and desired output.

Happy coding

This is what I have come up with. The idea is the user selects a file and the application returns the max value in a column along with the adjacent value. I have changed the file type to .txt but would like it to be able to handle both .txt and .csv.
In the code I have made some comments, I am very new to this and am starting to feel I have bitten off more than I can chew as I am learning this essentially through trial and error. I am having trouble getting the max value from column 1 and printing it along with the corresponding value in first column to a message box. I can get the whole line but not those specifically.

Sample data-
MD INC
0.0 0.0
86.65 0.44
115.600 1.88
143.45 5.5500

Python code[# The user selects a file they want to search, in this case a .txt but I would like to set it up so .csv can be searched as well
def OnButton(self, evt):
filedialog = wx.FileDialog(self,
message = 'Open text file',
defaultDir = '.',
defaultFile = 'TestTOC.txt',
wildcard = 'Text file (*.txt;*.txt)| *.txt;*.txt',
style = wx.OPEN)
if filedialog.ShowModal() == wx.ID_OK:
self.path = filedialog.GetPath()
self.fileisopen = True

def OnMax(self, evt):
if not self.fileisopen:
msgbox = wx.MessageDialog(self.background, message="No file is open!", style=wx.OK)
if msgbox.ShowModal() == wx.ID_OK:
msgbox.Close(True)


else:
self.myOK = True
#store the values in here
newlist=[]
#split the data based on white space, is it possible to set this up to split the data depending on the separator for eg a comma or white space
for line in file(self.path, 'r'):
data_list = line.split()
#search through column 1 which is the INC column
INC = float(data_list[1])
# find the max value and return it and the corresponding value in the adjacent column
if INC == >mx:
newlist.append(line [1],[0])

# print the result to a meassage box
tempresStr1 = "The maximum inclination is %.2f at ?.2f \n" % newlist
self.tempres.SetValue(tempresStr1)

]

Any tips or pointers would be greatly appreciated
Thank you

Sorry the previous post lost the formatting

# The user selects a file they want to search, in this case a .txt but I would like to set it up so .csv can be searched as well
    def OnButton(self, evt):
        filedialog = wx.FileDialog(self,
            message = 'Open text file',
            defaultDir = '.',
            defaultFile = 'TestTOC.txt',
            wildcard = 'Text file (*.txt;*.txt)| *.txt;*.txt',
            style = wx.OPEN)
        if filedialog.ShowModal() == wx.ID_OK:
            self.path = filedialog.GetPath()
            self.fileisopen = True

    def OnMax(self, evt):
        if not self.fileisopen:
            msgbox = wx.MessageDialog(self.background, message="No file is open!", style=wx.OK)
            if msgbox.ShowModal() == wx.ID_OK:
                msgbox.Close(True)

    

                
        else:
            self.myOK = True
            #store the values in here          
            newlist=[]
            #split the data based on white space, is it possible to set this up to split the data depending on the separator for eg a comma or white space
            for line in file(self.path, 'r'):
                data_list = line.split()
                #search through column 1 which is the INC column
                INC = float(data_list[1])
                # find the max value and return it and the corresponding value in the adjacent column 
                if INC == >mx:
                    newlist.append(line [1],[0])
                    
            # print the result to a meassage box            
            tempresStr1 = "The maximum inclination is %.2f at ?.2f \n" % newlist 
            self.tempres.SetValue(tempresStr1)

This is not pretty but it is working.

#store the values in here          
            newlist=[]
            #split the data based on white space, is it possible to set this up to split the data depending on the separator for eg a comma or white space
            for line in file(self.path, 'r'):
                md, inc = line.split()
                inc = float(inc)
                         
                newlist.append ( (inc, md) )
            
            # print the result to a meassage box            
            tempresStr1 = "The maximum inclination is %-1s degrees at %s metres \n" % (inc, md)
            self.tempres.SetValue(tempresStr1)

The only problem I have now is with reading the actual text file where there are random spaces and text on the first two rows and the actual data has a tab preceding the first column and two tabs in between the next 14 columns of which I am only interested in the first two

better to replace then line 5 with

md, inc =line.rsplit(" ",1)

To only separate the wanted last number from the end.

Then you can do for datalines:

md='sdfa asdfas fwer\t2334.34234\t\t342.453\t\t34234.3242\t\t42472.234543'

sep='\t\t'

if sep in md:
    firstcol, _, rest = md.partition(sep)
    _, firstcol = firstcol.rsplit('\t',1)
    secondcol, _ , rest = rest.partition(sep)

print float(firstcol),float(secondcol)

""" Output:
2334.34234 342.453
"""

Thanks for the info Tony, I think I can see where that would go, at the moment I have this and while it is doing everything req'd its not ideal. I know there must be an easy way to pass the data from the strip and split to the newlist but I cannot get it to work

newlist=[]
            inp = open(self.path)     
            outp = open('outp.txt', 'w')
            for eachline in inp:
                eachline = eachline.strip()
                line_data=eachline.split()
                outp.write ( '%s %s\n' % ( line_data[1], line_data[0] ) )
            inp.close()
            outp.close()

            for line in file('outp.txt', 'r'):
                inc, md = line.split()
                inc = float(inc)
                md = float (md)
                         
                newlist.append ( (inc, md) )
                          
            # print the result to a message box            
            tempresStr1 = "# The maximum inclination is %.2f degrees at %.2f metres. \n" % max(newlist)
            self.tempres.SetValue(tempresStr1)

Just as console proof:

outp = open('outp.txt', 'w')
for eachline in open('inp.txt'):
    if eachline[0].isdigit():
        outp.write(' '.join(eachline.split()[:2])+'\n')
outp.close()

print open('outp.txt').read()

newlist = [(float(a),float(b)) for a,b in [line.split() for line in open('outp.txt') ]]
print newlist
# print the result to a message box            
tempresStr1 = "# The maximum inclination is %.2f degrees at %.2f metres. \n" % (max(newlist))
##self.tempres.SetValue(tempresStr1)
print tempresStr1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.