I would like to convert specific ascii file to csv.
This ascii file has own specification and I post below relevant fragments.
line A starts with code 66TP:
66TP 1003 54.437269600149717.012388003107655.5139691177756 :10.008677993245250.01231534739191
line B starts with C6NM:
C6NM0821.565823793411260.900167346000671.2812114953994820.81007696688170033 1679475490.0000000001679475527.0000000001
As you see above individual values are not separated but their differentiation is
by position and length in line.
spec of line A:
1 Position Length Data format Description of field
2 1 2 Type code Record type code = 66
3 3 2 Derivation Derivation code
4 5 16 Name Point name
5 21 16 Latitude Latitude
6 37 16 Longitude Longitude
7 53 16 Distance WGS84 ellipsoidal height at APC
8 69 16 Text 16 Feature code
9 85 1 GPS Method Measurement method
10 86 1 Classification Classification of the point
11 87 16 Distance Horizontal precision
12 103 16 Distance Vertical precision
spec of line B:
1 Position Length Data format Description of field
2 1 2 Type code Record type code = C6
3 3 2 Derivation Derivation code
4 5 2 Integer 2 Minimum number of satellites
5 7 1 Boolean Relative DOPs
6 8 16 Scalar PDOP (maximum)
7 24 16 Scalar HDOP (maximum)
8 40 16 Scalar VDOP (maximum)
9 56 16 Scalar RMS
10 72 4 Integer 4 Number of GPS positions used
11 76 16 Distance Horizontal standard deviation
12 92 16 Distance Vertical standard deviation
13 108 4 Integer 4 Start GPS week
14 112 16 Scalar Start GPS time in seconds to 3dp
15 128 4 Integer 4 End GPS Week
16 132 16 Scalar End GPS time in seconds to 3dp
17 148 1 Monitor Status
my desired output is to merge both lines and would be like:
1003,54.4372696001497,17.0123880031076,55.5139691177756,0.009,0.012,8,1.6,0.9,1.28,20.8,033,1679,475490.0,1679,475527.0
and here’s input file where I marked individual values with square brackets:
66TP [1003] [54.4372696001497][17.0123880031076][55.5139691177756] :1[0.00867799324525][0.01231534739191]
C6NM[082][1.56582379341126][0.90016734600067][1.28121149539948][20.81007696688170][033] [1679][475490.000000000][1679][475527.0000000001]
Sorry for quite long post but I have no idea how could I describe it in shorter way.
I am an amateur beginner programmer and I’d like to ask you for any hint that let me start
handling such type of data.
Since you know the position of each element on each line, use a string slice to grab each element.
For example,
To take this a step further, you could write a little function to split a line apart given a list of the lengths for the line.
Code
Output
This just returns a list of the data elements. You could takes this a step further by providing a variable name along with each length (
[[2,'type'], [2,'derivation'],...]) and change the function around a little so it returns adictinstead so you could then access it by usingthe_result['variable_name']A few ideas to play with. http://learnpythonthehardway.org/ would be a good thing for you to work through so you learn the basics of the language.