【发布时间】:2021-12-28 13:28:08
【问题描述】:
我有多个 txt 文件,如下所示:
[Level1]
Location = "London"
Type= "GTHY66"
Date = "16-11-2021"
Energy level = "Critical zero"
[Level2]
0.000 26.788
0.027 26.807
0.053 26.860
因此,我想从我读取/处理的每个文件中创建两个数据框(最终我将推送到数据库)。
level1中的dataframe需要为df_level1:
Location Type Date Energy
London GTHY66 16-11-2021 Critical zero
level1下的dataframe需要为df_level2:
Speed Energylevel
0.000 26.788
0.027 26.807
0.053 26.860
这是我尝试过的,但我卡住了:
energy_root= r'c:\data\Desktop\Studio\Energyfiles'
#create list of file paths
def read_txt_file(path):
list_file_path = []
for root, dirs, files in os.walk(path):
for file in files:
if file.endswith('.txt'):
file_name = os.path.basename(file)
file_path = os.path.join(root, file_name)
list_file_path.append(file_path)
return list_file_path
def create_df():
for file in read_txt_file(energy_root):
file_name = os.path.basename(file)
file_path = os.path.join(energy_root, file_name)
datetime = re.findall(r'_(\d{8}_\d{6})\.', file_name)[0]
with open(file_path, 'r+') as output:
reader = output.readlines()
for row in reader:
d = row.split('=')
if len(d) > 1:
df_level1 = pd.DataFrame([d[1]], columns=[d[0]])
print(df_level1 )
"then create df_level2 ....."
create_df()
【问题讨论】:
标签: python pandas database list csv