473,394 Members | 1,750 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

first field criteria to new file

29
need to break out a file into three separate files based on first field e.g.

file = ['aaa sfsfs sfsdfs sdfes'
aaa srgfr grg ht hrhr
aaa egero eriore erore',
hh dsrgr rger ergerl rr
hh oefsrf werf ewf wfre,
k efwgwe wfwer fwe fwetf,
ddd fsff rgferw grwr
ddd wfrewe gptrg rer
ddd eortwer writwer erwo,
m fwewi qoovsd sjrrs fe ,]

so go through file and lines where the first three consecutive lines have the same first field send those three lines to file1, lines where only two cosecutive lines have the same first fields send to file 2 and rest which is only 1 unique first field to file 3 - so lines aaa ... and ddd ....would be in file1 etc..etc..

any idea how to read thru a file and do this is very much appreciated - Thanks
Nov 28 '09 #1
1 1471
kudos
127 Expert 100+
There is some unclearities here, however I guess the way to go would be the following:

Expand|Select|Wrap|Line Numbers
  1.  
  2. filec = {}
  3. for f in file:
  4.  key  =""
  5.  for r in range(3):
  6.   key+=f[r]
  7.  if(filec.has_key[key]):
  8.   filec[key]+=f+"\n"
  9.  else:
  10.   filec[key]=f+"\n"
  11.  
  12. for k in filec.keys():
  13.  f = open(k,"w")
  14.  f.write(filec[k])
  15.  f.close()
  16.  
(I haven't had the possiblity to test the code, but it should be something like this)

-kudos
Nov 28 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: John young | last post by:
I have been looking for an answer to a problem and have found this group and hope you can assist . I have been re doing a data base I have made for a car club I am with and have been trying to...
5
by: tdmailbox | last post by:
I have a form with a child form. In the child form there is a list of names that can grow quite large. On the parent form I want to display the first name from the child form. I set up a test...
1
by: S. van Beek | last post by:
Dear reader, How can I filter a numeric field with Like as criteria in a query. To filter a numeric field with <10 as criteria this will com back with the result of those records for which...
0
by: MLH | last post by:
I have an A97 query (qryVehiclesNowners2) that has a table field in it named . Depending on the selections made in a number of criteria choices on a form, a field on the form will have string...
2
by: David - Australia | last post by:
G'day from Australia, I'm hoping some bright spark may be able to help me with this one. I'm sure that it can be done, I've just hit a wall with it. So I'm opening it up. I'm storing student...
7
by: Mary | last post by:
I have a student who has a hyphenated first name. If I concatenate the name like this: StudentName:( & ", " & ), it works as expected. If, however, I try to get the first name first by...
9
by: bullockbefriending bard | last post by:
i have a large collection of python objects, each of which contains an integer 6-tuple as part of its data payload. what i need to be able to do is select only those objects which meet a simple...
9
by: hrreece | last post by:
I have an Access 2002 database that has a form that can be used to review individual records. At the bottom of the form are buttons that are linked to functions that allow the user to "Find a record...
7
by: DeZZar | last post by:
Hi all, Unfortunately I am quite a novice with Access!! I've created a number of data bases for my work however becuase my skills are limited to really built in functionality and wizards my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.