Skip to main content
 首页 » 编程设计

python之打开多个txt文件并执行相同的操作

2024年10月24日14oomusou

我有大约 50 个文本文件,我想打开它们,然后对其执行一些操作,然后将输出保存到一个新文件中。因此,对于这些文本文件中的 1 个,此代码可以满足我的要求:

#open file 
df=pd.read_csv(r'F:\Sheyenne\Statistics\NDVI_allotment\Text\A_Annex.txt', sep='\t', nrows=80, skiprows=2) 
 
#replace value names in 'Basic Stats' 
df=df.replace({'Band 80$': 'LT50300281984137PAC00', 
'Band 79$': 'LT50300281984185XXX15', 
'Band 78$': 'LT50300821984249XXX03', 
'Band 77$': 'LT50300281985139PAC12', 
'Band 76$': 'LT50300281985171PAC04', 
'Band 75$': 'LT50300281986206XXX03', 
'Band 74$': 'LT50300281986238XXX03', 
'Band 73$': 'LT50300281987241XXX04', 
'Band 72$': 'LT50300281987257XXX03', 
'Band 71$': 'LT50300281987273XXX05', 
'Band 70$': 'LT50300281988212XXX03'}, regex=True) 
 
#take a slice of the data 
df['Basic Stats']=df['Basic Stats'].str.slice(13,20) 
 
#sort the data 
df=df.sort(columns='Basic Stats', axis=0, ascending=True) 

我需要对所有 50 个文件执行这些完全相同的操作,有没有办法在 pandas 中执行此操作?不过,即使是非 Pandas 答案也会有所帮助。

编辑:

文件前 1000 个字符的片段:

'Filename: F:\\Sheyenne\\Atmospherically Corrected Landsat\\Indices\\Main\\NDVI\\NDVI_stack\nROI: EVF: Layer: Main_allotments.shp (allotment1=A. Annex) [White] 3984 points\n\nBasic Stats\t      Min\t     Max\t    Mean\t   Stdev\t  Num\tEigenvalue\n     Band 1\t 0.428944\t0.843916\t0.689923\t0.052534\t    1\t  0.229509\n     Band 2\t-0.000000\t0.689320\t0.513170\t0.048885\t    2\t  0.119217\n     Band 3\t 0.336438\t0.743478\t0.592622\t0.052544\t    3\t  0.059111\n     Band 4\t 0.313259\t0.678561\t0.525667\t0.048047\t    4\t  0.051338\n     Band 5\t 0.374522\t0.746828\t0.583513\t0.055989\t    5\t  0.027913\n     Band 6\t-0.000000\t0.749325\t0.330068\t0.314351\t    6\t  0.022561\n     Band 7\t-0.000000\t0.819288\t0.600136\t0.170060\t    7\t  0.018126\n     Band 8\t-0.000000\t0.687823\t0.450559\t0.084678\t    8\t  0.012942\n     Band 9\t 0.332637\t0.776398\t0.549870\t0.085212\t    9\t  0.009261\n    Band 10\t 0.386589\t0.848977\t0.635024\t0.087712\t   10\t  0.006628\n    Band 11\t 0.265165\t0.822361\t0.594286\t0.075730\t   11\t  0.004517\n    Band 12\t 0.191882\t0.539559\t0.343836\t0.0' 

编辑:

这段代码:

d={'Band 80$': 'LT50300281984137PAC00', 
'Band 79$': 'LT50300281984185XXX15', 
'Band 78$': 'LT50300821984249XXX03', 
'Band 77$': 'LT50300281985139PAC12', 
'Band 76$': 'LT50300281985171PAC04', 
'Band 75$': 'LT50300281986206XXX03', 
'Band 74$': 'LT50300281986238XXX03', 
'Band 73$': 'LT50300281987241XXX04', 
'Band 72$': 'LT50300281987257XXX03', 
'Band 71$': 'LT50300281987273XXX05', 
'Band 70$': 'LT50300281988212XXX03'} 
 
pth = r'F:\Sheyenne\Statistics\NDVI_allotment\Text' # path to files 
new = os.path.join(pth,"new")  
os.mkdir(new)  # create new dir for new files 
os.chdir(new) # change to that directory 
# loop over each file and update 
for f in os.listdir(pth): 
    df = pd.read_csv(os.path.join(pth, f), sep='\t', nrows=80, skiprows=2) 
    df = df.replace(d) 
    df['Basic Stats'] = df['Basic Stats'].str.slice(13,20) 
    df.sort(columns='Basic Stats', axis=0, ascending=True, inplace=True) 
    # save data to csv 
    df.to_csv(os.path.join(new, "new_{}".format(f)), index=False, sep="\t") 
print 'Done Processing' 

返回:

IOError: Initializing from file failed 

请您参考如下方法:

 d = {'Basic Stats':{'Band 80$': 'LT50300281984137PAC00', 
 'Band 79': 'LT50300281984185XXX15', 
 'Band 78': 'LT50300821984249XXX03', 
 'Band 77': 'LT50300281985139PAC12', 
 'Band 76': 'LT50300281985171PAC04', 
 'Band 75': 'LT50300281986206XXX03', 
 'Band 74': 'LT50300281986238XXX03', 
 'Band 73': 'LT50300281987241XXX04', 
 'Band 71': 'LT50300281987273XXX05', 
 'Band 70': 'LT50300281988212XXX03'}} 
 
 
pth = r'F:\Sheyenne\Statistics\NDVI_allotment\Text' # path to files 
new = os.path.join(pth,"new")  
os.mkdir(new)  # create new dir for new files 
# loop over each file and update 
for f in os.listdir(pth): 
    df = pd.read_csv(os.path.join(pth, f), sep='\t', nrows=80, skiprows=2) 
    df = df.replace(d) 
    df['Basic Stats'] = df['Basic Stats'].str.slice(13,20) 
    df.sort(columns='Basic Stats', axis=0, ascending=True, inplace=True) 
    # save data to csv 
    df.to_csv(os.path.join(new, "new_{}".format(f)), index=False, sep="\t") 

没有意义的部分是用字典中的值替换,然后切掉一些字符串,使用正确的值开始会更有意义。另一个问题是,如果 df['Basic Stats'] = df['Basic Stats'].str.slice(13,20) 不匹配任何内容,那么从 13:20 开始切片将留下一个空字符串所以你应该确保每一行肯定会有匹配,否则你最终会丢失数据