Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
404 views
in Technique[技术] by (71.8m points)

python - How can I read the comments in the csv files and use them?

#StationCode:4845    
#InstrumentKind: CSV  (20191216114518_4845)    
#StartTime:2019/12/16 11:45:28.010    
#RecordLength(sec):103.00    
#SampleRate(Hz): 100    
#AmplitudeUnit:  gal. DCoffset(corr)    
#AmplitudeMAX. a:   15.133 ~   -22.371    
#AmplitudeMAX. b:   70.222 ~   -37.683    
#AmplitudeMAX. c:   13.398 ~   -11.425    
#Intensity:5.0 touC:0.059 PGA:74.4gal PGV:27.7mm/s    
#Time sync.:NTP    
#DataSequence: Time; a; b; c; Pd; Displacement    
#Data: 4F10.3   

So this is how I have the csv file and these comments are situated at the beginning of the file. Now how can i possibly parse these comments one by one and use them? I came across articles that only helped me strip or ignore these comments while reading the csv file. Correct me if i am mistaken anywhere.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Its not clear what you mean by using them one by one. but to get these values into a list. you can use regex.

depending on your needs you can process the comments further to clean them up.

Added a simple comprehension to clean the spaces and add to a dictionary.


s = '''#StationCode:4845    
#InstrumentKind: CSV  (20191216114518_4845)    
#StartTime:2019/12/16 11:45:28.010    
#RecordLength(sec):103.00    
#SampleRate(Hz): 100    
#AmplitudeUnit:  gal. DCoffset(corr)    
#AmplitudeMAX. a:   15.133 ~   -22.371    
#AmplitudeMAX. b:   70.222 ~   -37.683    
#AmplitudeMAX. c:   13.398 ~   -11.425    
#Intensity:5.0 touC:0.059 PGA:74.4gal PGV:27.7mm/s    
#Time sync.:NTP    
#DataSequence: Time; a; b; c; Pd; Displacement    
#Data: 4F10.3 '''

import re

comments = re.findall(r'#(.*)', s)

keys = [x.split(':')[0].strip() for x in comments]
values = [x.split(':')[1].strip() for x in comments]

comments = dict(zip(keys,values))

for k,v in comments.items():
    print(f"key: {k} ---- value: {v}")


# Output: 
#key: StationCode ---- value: 4845
#key: InstrumentKind ---- value: CSV  (20191216114518_4845)
#key: StartTime ---- value: 2019/12/16 11
#key: RecordLength(sec) ---- value: 103.00
#key: SampleRate(Hz) ---- value: 100
#key: AmplitudeUnit ---- value: gal. DCoffset(corr)
#key: AmplitudeMAX. a ---- value: 15.133 ~   -22.371
#key: AmplitudeMAX. b ---- value: 70.222 ~   -37.683
#key: AmplitudeMAX. c ---- value: 13.398 ~   -11.425
#key: Intensity ---- value: 5.0 touC
#key: Time sync. ---- value: NTP
#key: DataSequence ---- value: Time; a; b; c; Pd; Displacement
#key: Data ---- value: 4F10.3



与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...