Utility pro DAKOS import
This commit is contained in:
		
							parent
							
								
									2270aeea51
								
							
						
					
					
						commit
						9cbaf696ec
					
				
					 2 changed files with 35 additions and 12 deletions
				
			
		
							
								
								
									
										22
									
								
								seminar/dksdump/helpers.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								seminar/dksdump/helpers.py
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,22 @@ | |||
| from seminar import ovvpfile | ||||
| import os | ||||
| 
 | ||||
| 
 | ||||
| def read_all_tables(basedir): | ||||
|     tables = {} | ||||
|     for fn in os.listdir(basedir): | ||||
|         if fn.endswith('.csv'): | ||||
|             print "Reading %s ..." % (fn, ) | ||||
|             with open(os.path.join(basedir, fn), 'r') as f: | ||||
|                 o = ovvpfile.parse(f, with_headers=False) | ||||
|             tables[fn[:-4]] = o.rows | ||||
|             print "  %d lines, columns: %s" % (len(o.rows), ' '.join(o.columns), ) | ||||
|     return tables | ||||
| 
 | ||||
| def matchrows(tab, key, val): | ||||
|     return [r for r in tab if r[key]==val] | ||||
| 
 | ||||
| def onerow(tab, key, val): | ||||
|     t = matchrows(tab, key, val) | ||||
|     assert(len(t) == 1) | ||||
|     return t[0] | ||||
|  | @ -20,7 +20,7 @@ class OvvpFile(object): | |||
|     def to_string(self): | ||||
|         return ''.join(self.to_lines()) | ||||
| 
 | ||||
|     def parse_from(self, source): | ||||
|     def parse_from(self, source, with_headers=True): | ||||
|         "Parse data from file, string or line iterator, overwriting self" | ||||
|         if isinstance(source, str) or isinstance(source, unicode): | ||||
|             return self.parse_from(source.split('\n')) | ||||
|  | @ -29,15 +29,16 @@ class OvvpFile(object): | |||
| 
 | ||||
|         # header | ||||
|         self.headers = {} | ||||
|         for r in it: | ||||
|             if isinstance(r, str): | ||||
|                 r = r.decode('utf8') | ||||
|             assert isinstance(r, unicode) | ||||
|             r = r.rstrip('\n') | ||||
|             if r == u"": | ||||
|                 break | ||||
|             k, v = r.split(u'\t', 1) | ||||
|             self.headers[k] = v | ||||
|         if with_headers: | ||||
|             for r in it: | ||||
|                 if isinstance(r, str): | ||||
|                     r = r.decode('utf8') | ||||
|                 assert isinstance(r, unicode) | ||||
|                 r = r.rstrip('\n') | ||||
|                 if r == u"": | ||||
|                     break | ||||
|                 k, v = r.split(u'\t', 1) | ||||
|                 self.headers[k] = v | ||||
| 
 | ||||
|         # columns | ||||
|         r = it.next() | ||||
|  | @ -61,7 +62,7 @@ class OvvpFile(object): | |||
|          | ||||
| 
 | ||||
| 
 | ||||
| def parse(source): | ||||
| def parse(source, with_headers=True): | ||||
|     o = OvvpFile() | ||||
|     o.parse_from(source) | ||||
|     o.parse_from(source, with_headers=with_headers) | ||||
|     return o | ||||
|  |  | |||
		Loading…
	
		Reference in a new issue
	
	 Tomas Gavenciak
						Tomas Gavenciak