2020-10-19 Python opens text to extract data

# !/usr/bin/env python
#  -*- coding: utf-8 -*-
# Author:
# Date:2020.10.13   23:31
#Description: open a txt including unrelvant characters(titles) and exiperiment raw data(f(x)  two-column data like    adsorbtion versus wavelength in UV-vis) and give a plot.
import tkinter as tk
from tkinter.filedialog import askopenfilename
from itertools import islice
import sys
import fileinput
import matplotlib.pylab as pylab
#import numpy as np
#import matplotlib.pyplot as plt
data_file_path = askopenfilename(title='Select a data',filetypes=[('TXT','*.txt')],initialdir='C:\\Users\\zhuli\\Desktop\\20201013')
print(data_file_path)
#f1 = open(data_file_path,'r')
#E:\00 Experiment\20201013
#islice(seq[, start], stop[, step]);
from itertools import islice
#with open(r'E:\\xx\\movie2.txt', encoding='utf-8', ) as txtfile:
data=[]
with open(data_file_path ) as txtfile:
    line = txtfile.readlines()
    for i, rows in enumerate(line):
        if i in range(58, len(line)-1):  # 指定数据行
            #print(rows)
            data.append(rows)
print("length", len(data))
txtfile.close()
# 写入txt
with open('C:\\Users\\xxxx\\Desktop\\20201013\\6-1.dat', "w", ) as f:
    for i in range(len(data)):
        f.writelines(data[i])
    f.close()

#Reference   http://www.360doc.com/content/19/0828/10/47812380_857519286.shtml
#Say thanks to the author of the docment.

Guess you like

Origin blog.csdn.net/you_us/article/details/109170850