我是python编码的新手。我在读取多个.txt文件时需要帮助。
我大约有50个.txt文件,每个文件包含30列。
我在第25列中搜索一个值,并在第1列中获得相应的值。
应该用于目录中的所有文件。
谢谢。
当前代码为:
import glob
import os
import numpy as np
import csv
import matplotlib.pyplot as plt
file_list = glob.glob('*.txt')
for file_path in data:
for i in range(250,300,50):
First_start_pcls=5500 # rows
First_end_pcls=6000 #rows
for d in file_list:
a_s=(d[First_start_pcls:First_end_pcl,1])
b_s=(d[First_start_pcls:First_end_pcl,25])
但是,我从这里受到了打击。
我需要知道类似x>-72 & X<-73
的值。在5500:6000
行之间
然后打印第1(a),25(v)列
txt文件如下所示。
0 0.100002 0.100005 0.1 750 0 0.15 150 0 0 0.09999 5000 10 -1.79126e-06 -9.70263e-10 0.285642 -0.0212535 0 0 1.0251 0.00229463 0.00061384 -1.39956e-06 0 0.00168796 -79.2124 0.000385773 0.00109869 0 0 180
1 0.100375 0.100805 0.100233 750.028 3.25521e-06 0.149993 150 8.43446e-11 3.86358e-14 0.099039 5000 10 -26.1624 -0.201879 0.477011 0.142304 0 -0.00478525 4.66682e-10 0.489863 0.0755384 7.18662e-05 0.000464599 0.0328732 38.385 0.000387414 0.0010954 0 0 180.043
2 0.100732 0.102105 0.105489 750.106 2.79018e-06 0.149986 150 9.00251e-10 8.79657e-13 0.0981785 5000 10 -0.00916317 -0.193042 0.477412 0.150787 0 -0.0620785 3.08304e-10 1.92367 0.28177 0.000263186 0.00183184 0.0332253 39.4411 0.000388996 0.00109228 0 0 180.085
3 0.101075 0.103434 0.1177 750.224 6.97545e-06 0.149979 150 3.1313e-09 4.85265e-12 0.0973999 5000 10 -3.72894e-06 -0.21107 0.476546 0.148985 0 -0.0820207 7.45346e-10 2.27915 0.375781 0.000542299 0.00339836 0.0324755 36.5965 0.000390519 0.00108931 0 0 180.126
4 0.101405 0.104825 0.132947 750.376 1.95313e-05 0.149972 150 7.05902e-09 1.5288e-11 0.0966955 4999.99 10 -1.55684e-09 -0.230125 0.475581 0.146907 0 -0.104142 1.89863e-09 2.28998 0.409257 0.000881221 0.00511384 0.0316808 33.5859 0.000391987 0.00108649 0 0 180.166
5 0.101722 0.106308 0.14629 750.554 4.13876e-05 0.149965 150 1.27143e-08 3.58097e-11 0.0960581 4999.99 10 -6.68377e-13 -0.248602 0.474587 0.145348 0 -0.136107 4.71837e-09 2.19423 0.41199 0.00125702 0.00695662 0.0309066 30.6545 0.0003934 0.00108381 0 0 180.205
答案 0 :(得分:0)
#change this if you have a header and use the proper separator according to your text file
df= pd.read_csv('inputfile.txt', sep=" ", header=None)
#this if you actually have a column named 'x', for which you want to check if the values are between 73 and 74.
df=df[df['x'].between(73,74)].loc[5499:5999]
#if your columns are unnamed and you know which column you want to check (say the second column, which has index 1)
df=df[df[:,1].between(73,74)].loc[5499:5999]
print(df.iloc[:,0])
print(df.iloc[:,24])
给出OP评论
import glob
import os
import numpy as np
import csv
import pandas as pd
import matplotlib.pyplot as plt
dff = pd.DataFrame()
for filename in glob.iglob("*.txt"):
df = pd.read_csv(filename, sep=" ", header=None)
df=df[df[25].between(-73,-72)].loc[7999:9999]
dff=dff.append([df.iloc[:,0],df.iloc[:,4]])
print(df.iloc[:][0])
print(df.iloc[:][25])
答案 1 :(得分:0)
您可以尝试创建一个txt文件,并使用该程序在每一行上编写要打开的文本文件:
例如
test.txt
dics.txt
fruit.txt
code.txt
...
然后,您应该创建一个for循环,以从创建的txt文件中选择每个文件名并读取它:
例如
files = open("filenames.txt","r")
for file in files:
x = open(file,"r").read()
print(x)
我希望能为您提供帮助。