Can I put a variable in the path in Python?

Asked 2 years ago, Updated 2 years ago, 20 views

import json
import pandas as pd

with open('D:\\openpose\\train_json\\prame (1).json', 'r') as f:

json_data = (json.load(f))
json_ar = json_data.get('people')


f or list in json_ar:
a = (list.get("pose_keypoints_2d")),

print(a)

df = pd.DataFrame(a)

df.to_excel('xy.xlsx',index=False)

This is my code, and that frame (1).There are hundreds of files called json, and the difference in the name of the file is that There are only numbers, so I want to call them up one by one, add the list to Excel, and send it out Is there a way? And I want to add data by adding rows instead of overwriting to Excel ㅠ<

python

2022-09-20 17:02

2 Answers

You can put the path name in the for statement and then work on it.

A = []
for i in ['D:\\openpose\\train_json\\prame (1).json', 'path name 1', 'path name 2', etc..]:
    with open(i, 'r') as f:
        A.append(f)


2022-09-20 17:02

I think you're dealing with Python because you need something for the project, but I think it'll save time if you go through the basic grammar once.

Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> for i in range(1, 10):
    print(f"D:\\openpose\\train_json\\prame ({i}).json")


D:\openpose\train_json\prame (1).json
D:\openpose\train_json\prame (2).json
D:\openpose\train_json\prame (3).json
D:\openpose\train_json\prame (4).json
D:\openpose\train_json\prame (5).json
D:\openpose\train_json\prame (6).json
D:\openpose\train_json\prame (7).json
D:\openpose\train_json\prame (8).json
D:\openpose\train_json\prame (9).json


2022-09-20 17:02

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.