I have a csv that look like this:
I use this code:
pairs_ = dict()
with open('text2.txt', 'r') as file:
for i, j in re.findall(r'(Layer_\d+)\s,\s(\d+\.\d+)ms', file.read()):
pairs_.setdefault(i, []).extend([i, j])
df = pd.DataFrame(pairs_.values())
to make the pairs aligned in parallel.\
however I need to add the *** Golden value for Channel[%d], CE[%d] ***
on top of every set before the Layer_00
does anyone know how to achieve it? (the number of sets maybe different, this is just an example of 3 sets)
Just create your headers in a list and pass that to DataFrame, remembering to leave a gap for your intermediate columns. That won't be possible using the mechanism you have now, where you do everything in one re.findall
call. You'll have to process line by line so you can extract the headers. This is untested:
pairs_ = dict()
titles = []
with open('text2.txt', 'r') as file:
for line in file:
if line.startswith('***'):
titles.extend( [line.rstrip(),str(len(titles))] )
else:
for i, j in re.findall(r'(Layer_\d+)\s,\s(\d+\.\d+)ms', line):
pairs_.setdefault(i, []).extend([i, j])
df = pd.DataFrame(pairs_.values(), columns=titles )
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.