I'm trying to read multiple files, converting to a dictionary and then save it into a csv. This is what I did, but I only keeps the last iteration of the files. ''' list_keys = ["hostname", "linuxVersion", "ifconfig", "listApplications", "nonJavaProcess", "javaProcess", "tomcatIfRunning", "tomcatVersion"] files = glob.glob('data/*', recursive=True)
new_data = []
for file in files:
with open(file, 'r') as json_file:
data = json_file.read()
split_json = data.split('][')
new_data.append(split_json)
dictionary = {}
final_dictionary = {}
for list in new_data:
dictionary = dict(zip(list_keys, list))
final_dictionary.update(dictionary)
dataframe = pandas.DataFrame(final_dictionary, index=[0], columns=list_keys)
dataframe.to_csv("relevamiento.csv", index=False)
''' The formating is OK in the CSV, but I only have the last file I read.
any ideas? Thanks!!
CodePudding user response:
You need to append the dict in a dataframe to the large dataframe
new_data = []
dictionary = {}
final_dictionary = {}
dataframe = pandas.DataFrame()
for file in files:
with open(file, 'r') as json_file:
data = json_file.read()
split_json = data.split('][')
new_data.append(split_json)
for list in new_data:
dictionary = dict(zip(list_keys, list))
final_dictionary.update(dictionary)
df1 = pandas.DataFrame(final_dictionary, index=[0], columns=list_keys)
dataframe = dataframe.append(df1)
dataframe.to_csv("relevamiento.csv", index=False)
if json isn't required, you can do it in a lot less code
dataframe = pandas.DataFrame()
for file in files:
df1 = pd.read_csv(file)
dataframe = dataframe.append(df1)
dataframe.to_csv("relevamiento.csv", index=False)