我正在尝试合并两个具有不同记录数的文件。每个文件的前150条记录应省略,而每个文件的后400条记录应合并为一个文件。由于我对python的有限了解,我编写的程序将进入无限循环。输入文件为json格式,而输出文件为csv。下面是我尝试过的示例数据输入文件。
import math
import json
#Acc code begin
fp=open("sitting.csv","w")
activity="sitting"
count=0
while count<650:
with open("acc.json") as data_file:
my_list1 = [json.loads(line) for line in data_file]
for acce in my_list1:
Xaxis = float(format(float(acce['dataX']),'.4f'))
Yaxis = float(format(float(acce['dataY']),'.4f'))
Zaxis = float(format(float(acce['dataZ']),'.4f'))
#Gyro code
with open("gyro.json") as data_file2:
my_list2 = [json.loads(line) for line in data_file2]
for gyro in my_list2:
pitch=float(format(float(gyro['dataX']),'.4f'))
roll=float(format(float(gyro['dataY']),'.4f'))
yaw=float(format(float(gyro['dataZ']),'.4f'))
# Start writing total 400 records in file starting from 150 records onwards
if count>150:
print activity,",",Xaxis,",",Yaxis,",",Zaxis,",",pitch,",",roll,",",yaw
print>>fp,activity,",",Xaxis,",",Yaxis,",",Zaxis,",",pitch,",",roll,",",yaw
count=count+1
fp.close()
print "Combined File Created"
我的实际输入数据集acc.json和gyro.json
https://drive.google.com/file/d/1kKez7DCVT-jL5NT3qlP_Xj8xm1oGq0x5/view?usp=sharing
https://drive.google.com/file/d/1zgExiiiDaLP1Hoa0wRGOfvljUQHj6Jko/view?usp=sharing
请提供帮助,并建议是否可以减少代码。
答案 0 :(得分:1)
我对您的意图和所做的每一步都不满意,但我尝试简化并解决主要问题。首先,缩进意味着while循环内count不会增加。其次,原本应该发生的事情需要从while循环中走出来。另外,我使用slice来消除线阵列中不需要的部分。但是我认为您希望从每个记录的每个数组中获得3个项目,因此我无法使用for循环进行打印。还需要在循环内进行打印。另外,我使用的在线编译器在要打印的参数周围使用括号。让我知道这是否需要调整,但是如果我理解您的话,它应该可以工作(假设您对我的工作没有改变,感到满意)。
import math
import json
#Acc code begin
fp=open("sitting.csv","w")
activity="sitting"
with open("acc.json") as data_file:
my_list1 = [json.loads(line) for line in data_file]
my_list1 = my_list1[150 : 550]
with open("gyro.json") as data_file2:
my_list2 = [json.loads(line) for line in data_file2]
my_list2 = my_list2[150 : 550]
count = 1
while count <=400:
acce = my_list1[count]
Xaxis = float(format(float(acce['dataX']),'.4f'))
Yaxis = float(format(float(acce['dataY']),'.4f'))
Zaxis = float(format(float(acce['dataZ']),'.4f'))
gyro = my_list2[count]
pitch=float(format(float(gyro['dataX']),'.4f'))
roll=float(format(float(gyro['dataY']),'.4f'))
yaw=float(format(float(gyro['dataZ']),'.4f'))
# Start writing total 400 records in file starting from 150 records onwards
print (activity,",",Xaxis,",",Yaxis,",",Zaxis,",",pitch,",",roll,",",yaw)
print>>fp(activity,",",Xaxis,",",Yaxis,",",Zaxis,",",pitch,",",roll,",",yaw)
count=count+1
fp.close()
print ("Combined File Created")