我试图从.txt文件中提取数据到我的csv文件。我的数据与流量工程有关。我已经包括了我的数据结构的屏幕截图。我需要从txt文件中获取队列长度和一些其他数据项。
下面是数据源SS。[
在此处输入图像说明]([https://i.stack.imgur.com/3q3Fd.png](https://i.stack.imgur.com/3q3Fd.png)) 我的输出目前看起来像这样
[在这里输入图像描述
](https://i.stack.imgur.com/qYJnF.png)
import csv
TOD_List = ['AM','PM']Years = 2025 #['2025','2026','2027','2028','2029',# '2030','2031','2032','2033','2034','2035','2036','2037','2038','2039',#'2040','2041','2042','2043','2044','2045']BEBR_List = ['Design']Folder = r"I:\TPA\PRJ\000011957\1.0_Task_Work_Orders_(TWOs)\1.5_TWO_5_081419\I75atSR52_Study\Traffic\B - Operational Analysis\test"Output = r"I:\TPA\PRJ\000011957\1.0_Task_Work_Orders_(TWOs)\1.5_TWO_5_081419\I75atSR52_Study\Traffic\B - Operational Analysis\test\test.csv"
Count=0Intersection = []
Col = []TM = []QL = []VC = []
with open(Output,'w',newline='') as OutCSV:fieldnames = ['TOD','Year','BEBR','Int','Mvmt','QL95','Stor','QL50']writer = csv.DictWriter(OutCSV, fieldnames=fieldnames)writer.writeheader()
for TOD in TOD_List:
Year =2025
for BEBR in BEBR_List:
Input_Report = "{}\\Volume_{}_{}_{}_Report.txt".format(Folder,TOD,Year,BEBR)
with open(Input_Report) as infile:
reader = csv.reader(infile, delimiter='\t')
for row in reader:
#print(row)
if (len(row)>0):
if (row[0] == 'Queues'):
Count=0
Sig=1
Unsig=0
Count +=1
#print(row)
#Pull intersection ID
if (Count==2):
Intersection = row[0]
Intersection = Intersection.split(':')[0]
#Turning Movement Name
if (row[0] == 'Lane Group '):
Num_Move = len(row)
for x in range(2,Num_Move):
Col.append(row[x])
#Turning Movement Volume
if (row[0] == 'Queue Length 95th (ft)'):
for x in range(2,Num_Move):
TM.append(row[x])
#Turning Movement Volume
if (row[0] == 'Queue Length 50th (ft)'):
for x in range(2,Num_Move):
QL.append(row[x])
#VC
if (row[0] == 'Turn Bay Length (ft) '):
for x in range(2,Num_Move):
VC.append(row[x])
#if (row[0] == 'Intersection Summary'):
#for x in range(0,Num_Move-2):
writer.writerow({'TOD': TOD,'Year': Year,'BEBR': BEBR,'Int': Intersection,'Mvmt': Col[x],'QL95': TM[x],'QL50': QL[x],'Stor': VC[x]})
Col = []
TM = []
QL = []
VC = []
print("Complete")
1条答案
按热度按时间k97glaaz1#
所以,这些都不是定位列。这些列由制表符分隔。这使得处理更容易一点。这将读取文件并为每个文件生成一个dataframe。也许这可以给予你一些开始。
输出(删节):