Commit d8dd4dc8 authored by vuong's avatar vuong

add convert file

parent e77f53d8
import pandas as pd
def csv_to_json(csv_file, json_file):
# read csv to dataframe
df = pd.read_csv(csv_file)
# Chuyển đổi DataFrame thành JSON
json_data = df.to_json(orient='records')
# Save data to JSON file
with open(json_file, 'w') as f:
f.write(json_data)
def csv_to_text(csv_file, text_file):
# read csv to dataframe
df = pd.read_csv(csv_file)
# Open File to write data
with open(text_file, 'w') as f:
# Convert DataFrame into text with template "Title: value"
for index, row in df.iterrows():
for col in df.columns:
f.write(f"{col}: {row[col]}\n")
csv_file_path = 'extra.csv'
json_file_path = 'converted_data.json'
# convert csv to json and save
csv_to_json(csv_file_path, json_file_path)
print("Convert to JSON successfully!")
# # convert CSV to JSON
# json_data = csv_to_json(csv_file_path)
# print("JSON Data:")
# print(json_data)
# text_file_path = 'converted_data.txt'
# # convert csv to text
# csv_to_text(csv_file_path, text_file_path)
# print("Convert to text successfully!")
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment