7 Replies - 3790 Views - Last Post: 30 December 2012 - 12:54 PM Rate Topic: -----

#1 beavez  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 18-December 12

Create New files in python

Posted 18 December 2012 - 03:29 AM

I have a list of Data X Y Z
#X Y Z
1 4 5
1 3 6
5 2 5
1 3 8
1 2 6

I want to create a different file for each line with the Same Z. For example, it should output 3 files from the one I gave it above. Group1_Z5 Group2_Z6, Group3_Z6
Format of Group1_Z5 will also be X Y Z
#X Y Z
1 4 5
5 2 5
Is This A Good Question/Topic? 0
  • +

Replies To: Create New files in python

#2 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,682
  • Joined: 13-March 10

Re: Create New files in python

Posted 18 December 2012 - 03:54 AM

How is your data stored in-memory?
Was This Post Helpful? 0
  • +
  • -

#3 beavez  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 18-December 12

Re: Create New files in python

Posted 18 December 2012 - 03:55 AM

View Postdarek9576, on 18 December 2012 - 03:54 AM, said:

How is your data stored in-memory?



I am not sure if I understand your question exactly, but my data has this format exactly
Everything separated by commas
X Y Z
-237,6928,7431
Was This Post Helpful? 0
  • +
  • -

#4 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,682
  • Joined: 13-March 10

Re: Create New files in python

Posted 18 December 2012 - 04:09 AM

My Question is whther the data is encapsulated in a class, is stored simply in a list, a dictionary etc.

Could you show us the code where you hold the values?

Is it in this kind of format?

lst = [ [1,2,3], [4,5,6], [7,8,9] ]


Was This Post Helpful? 0
  • +
  • -

#5 beavez  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 18-December 12

Re: Create New files in python

Posted 18 December 2012 - 04:20 AM

View Postdarek9576, on 18 December 2012 - 04:09 AM, said:

My Question is whther the data is encapsulated in a class, is stored simply in a list, a dictionary etc.

Could you show us the code where you hold the values?

Is it in this kind of format?

lst = [ [1,2,3], [4,5,6], [7,8,9] ]




It is stored simply in a list

det_id,exp_id,ra,dec,mjd_obs,l,b,flags,flags2,filterid,psf_inst_mag,psf_inst_mag_sig,psf_inst_flux,psf_inst_flux_sig,ap_mag,ap_mag_raw,ap_mag_radius,peak_flux_as_mag,cal_psf_mag,cal_psf_mag_sig,GroupID,GroupSize
-2371193182890077322,-2443338737858379774,358.8699414831746,0.1577604950965059,55452.4578684261,94.41417399488023,-59.579451041301894,35661824,0,i.0000,-9.293763,0.028860783,,,-10.178192,,12.0,-8.249554,19.587418,0.01746928,7431,2
-2371193182890077320,-2443338737858379774,358.8699423268292,0.15776082631590468,55452.4578684261,94.41417578110337,-59.5794510999456,270542848,0,i.0000,-9.293065,0.028875062,,,-10.174447,,12.0,-9.301054,19.588116,0.01746928,7431,2


....

I want to seperate them according to their "GroupID"
Was This Post Helpful? 1
  • +
  • -

#6 alexr1090  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 44
  • View blog
  • Posts: 124
  • Joined: 08-May 11

Re: Create New files in python

Posted 28 December 2012 - 03:04 AM

I plus 1 you for causing an error in d.i.c.displaying the text running off the page (on my screen at least). So what I got from you is that you'd like to create a file for each unique third value in your list, then append that file with any other third values that match it. Are your 'A, B, C' values separated by a comma in a long list, meaning that there are a lot of values in a list and after every third one © it starts over to A again? If it is this is what I'd do.
import os

#theList is basically your list of values
newList = theList.split(',')
for x in xrange(0, len(newList), 3): #note: might have to do a try except here if your length of list isn't divisible by 3
  a = newList[x]
  b= newList[x+1]
  c = newList[x+2]
  
  fn = str(c)+'.txt'
  #check if we already have this file. If we do, append it, otherwise, write it.
  if os.path.exists(fn):
    f = open(fn, 'a')
  else:
    f = open(fn, 'w')
  
  f.write(str(B)/>/>/>/>+','+str(c)) #assuming you want the values separated by commas
f.close()


If this isn't relevant please clarify! I'd like to help!

*edit*
I'm not sure why but it keeps changing my code from 'f.write(str(B)/>/>+','+str©)' to 'f.write(str(B)/>/>/>...'

*edit 2*
Oh I see why now. basically B should be lowercase and there shouldn't be a '/>/>' after it. No smilies please

P.S anyone know where I can report this bug that I'm proud to have found?

This post has been edited by alexr1090: 28 December 2012 - 03:14 AM

Was This Post Helpful? 0
  • +
  • -

#7 Tayacan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 275
  • Joined: 18-January 11

Re: Create New files in python

Posted 30 December 2012 - 08:22 AM

View Postalexr1090, on 28 December 2012 - 10:04 AM, said:

anyone know where I can report this bug that I'm proud to have found?


I found that one a year ago! Actually, I found it because I was making my own website at the time, and had the same problem. Then, when I tried to show some example input to D.I.C, the same thing happened!

Anyway, the cause it this: In html, the text in each post is contained in some element, which has the property that it wraps the text nicely. However, the text is broken up by spaces, so if there are no spaces, you get no wrapping - the text just continues.

Fixing this problem would require a script that checks if a "word" is too long to fit, and if it is, breaks it up - maybe with a hyphen. That's probably more trouble than it's worth, considering how rarely it happens, and it would kinda mess up stuff like the data posted here.
Was This Post Helpful? 1
  • +
  • -

#8 alexr1090  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 44
  • View blog
  • Posts: 124
  • Joined: 08-May 11

Re: Create New files in python

Posted 30 December 2012 - 12:54 PM

View PostTayacan, on 30 December 2012 - 09:22 AM, said:

View Postalexr1090, on 28 December 2012 - 10:04 AM, said:

anyone know where I can report this bug that I'm proud to have found?


I found that one a year ago! Actually, I found it because I was making my own website at the time, and had the same problem. Then, when I tried to show some example input to D.I.C, the same thing happened!

Anyway, the cause it this: In html, the text in each post is contained in some element, which has the property that it wraps the text nicely. However, the text is broken up by spaces, so if there are no spaces, you get no wrapping - the text just continues.

Fixing this problem would require a script that checks if a "word" is too long to fit, and if it is, breaks it up - maybe with a hyphen. That's probably more trouble than it's worth, considering how rarely it happens, and it would kinda mess up stuff like the data posted here.


Thanks I didn't know that. I was actually talking about reporting the bug that changed my code to the smiley face text automatically. I think ( don't know, think ) that the site automatically changes text to a smiley face code unless said text is in a code block. This is nice, however, they missed one thing. Although smileys are programmed not to occur in code, the text is still changed to a the smiley code, hence my lowercase b being changed to uppercase and having other unwanted changes in there.

Solution:
If it all works like I think it does there may be a function or something called automatically for all text. That function needs to not be called when inside a code tag.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1