1

New to programming in general. This is my code

for b in range(LengthSpread):
  Strip = ReadSpread[b].rstrip('\n')
  SplitData = ReadSpread[b].split(",")
  PlotID = SplitData[1]
  PlotIDnum = float(PlotID)
  if PlotIDnum == 1:
      List = SplitData
      print List
      OpenBlank.writelines('%s\n\n\n\n\n' % List)

Ultimately I want to find data based on changing each plotIDnum in the overall dataset. How would I change the number in the conditional if statement, without physically changing the number. Possibly using a for loop, or a while loop. Can't wrap my mind around it.

This is an example of the inputdata

09Tree #PlotID  PlotID  
1       1       Tree            
2       1       Tree        
3       2       Tree        
4       2       Tree        
6       4       Tree        
7       5       Tree        
8       5       Tree        
9       5       Tree        

I want my output to be organized by plotID#, and place each output in either a new spreadsheet or have each unique dataset in a new tab

Thanks for any help

4
  • Just a suggestion: throw all the values of PlotIDnum in an array and then iterate over that array. Commented Jul 16, 2013 at 15:59
  • 4
    I strongly encourage you to read up on python naming conventions, and follow them. The above code is almost maliciously unreadable. Commented Jul 16, 2013 at 15:59
  • 1
    On top of what @roippi 's suggestion, could you please include some sample input and desired output (if applicable)? Commented Jul 16, 2013 at 16:00
  • CamelCase and Python do not mix. Commented Jul 16, 2013 at 16:01

1 Answer 1

1

I'm not sure how exactly you would like to organize your files, but maybe you could use the plot ID as part of the file name (or name of the tab or whatever). This way you don't even need the extra loop, for example:

for b in range(length_spread):
    data = read_spread[b].rstrip('\n')
    splitted = data.split(',')
    plot_id = splitted[1]  # Can keep it as a string

    filename = 'plot_id_' + plot_id + '.file_extension'
    spreadsheet = some_open_method(filename, option='append')
    spreadsheet.writelines('%s\n\n\n\n\n' % splitted)
    spreadsheet.close_method()

Perhaps you could also make use of the with statement:

with some_open_method(filename) as spreadsheet:
    spreadsheet.writelines('%s\n\n\n\n\n' % splitted)

This ensures (if your file-object supports this) that the file is properly closed even if your program encounters an exception during writing to the file.

If you want to use some kind of extra loop I think this is the simplest case, assuming you know all the plot ID's beforehand:

all_ids = [1, 2, 4, 5]
# Note: using plot_id as integer now
for plot_id in all_ids:
    filename = 'plot_id_%i.file_extension' % plot_id
    spreadsheet = some_open_method(filename, option='write')
    for b in range(length_spread):
        data = read_spread[b].rstrip('\n')
        splitted = data.split(',')
        if plot_id == int(splitted[1]):
            spreadsheet.writelines('%s\n\n\n\n\n' % splitted)
    spreadsheet.close_method()
Sign up to request clarification or add additional context in comments.

2 Comments

All my plot_ids were from the same file as all my other data. So I had to readjust your code to fit mine. And it worked perfectly. I used your code with the extra loop, and using the array of all_ids. I just needed a little guidance to get my thinking down the right path. My variable names were a bit confusing before, I understand how to make my code more clear and readable. Thank you for all your help, I'm sure ill be back again.
No problem! I think the main problem with your variable names was that you used CapitalizedWords (= CamelCase), which in Python are generally used for class and exception names and special keywords like True and False, hence the highlighting in your code snippet. See here for more on naming conventions. Also don't use the name list in Python because that's the name of a very very common builtin type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.