Python 将 JsonL 转换为 CSV 文件时删除 .jsonl 扩展名
Python removing .jsonl extension when converting JsonL to CSV File
我有一个脚本,可以将选定目录中的 jsonl 文件转换为另一个指定位置的 csv 文件。但是,在将文件转换为 csv 格式后,最终创建的 csv 文件在 .csv 之前包含一个 .jsonl 扩展名(思考 file.jsonl.csv
)关于如何在后面添加 csv 扩展名之前删除 .jsonl 扩展名的任何想法?我希望我能够摆脱 csv 文件的 .jsonl 扩展名,因为它将来可能会造成混淆。谢谢!
创建的示例 CSV 文件:
20210531_CCXT_FTX_DOGEPERP.jsonl.csv
我的脚本:
import glob
import json
import csv
import time
start = time.time()
#import pandas as pd
from flatten_json import flatten
#Path of jsonl file
File_path = (r'C:\Users\Natthanon\Documents\Coding 101\Python\JSONL')
#reading all jsonl files
files = [f for f in glob.glob( File_path + "**/*.jsonl", recursive=True)]
i = 0
for f in files:
with open(f, 'r') as F:
#creating csv files
with open(r'C:\Users\Natthanon\Documents\Coding 101\Python\CSV\' + f.split("\")[-1] + ".csv", 'w' , newline='') as csv_file:
thewriter = csv.writer(csv_file)
thewriter.writerow(["symbol", "timestamp", "datetime","high","low","bid","bidVolume","ask","askVolume","vwap","open","close","last","previousClose","change","percentage","average","baseVolume","quoteVolume"])
for line in F:
#flatten json files
data = json.loads(line)
data_1 = flatten(data)
#headers should be the Key values from json files that make Column header
thewriter.writerow([data_1['symbol'],data_1['timestamp'],data_1['datetime'],data_1['high'],data_1['low'],data_1['bid'],data_1['bidVolume'],data_1['ask'],data_1['askVolume'],data_1['vwap'],data_1['open'],data_1['close'],data_1['last'],data_1['previousClose'],data_1['change'],data_1['percentage'],data_1['average'],data_1['baseVolume'],data_1['quoteVolume']])
问题是因为您在写入新文件时没有去掉扩展名,用这样的东西来代替您创建的 csv 文件应该可以解决这个问题
file_name = f.rsplit("\", 1)[-1].replace('.jsonl', '')
with open(r'C:\Users\Natthanon\Documents\Coding 101\Python\CSV\' + file_name + ".csv", 'w' , newline='') as csv_file:
我有一个脚本,可以将选定目录中的 jsonl 文件转换为另一个指定位置的 csv 文件。但是,在将文件转换为 csv 格式后,最终创建的 csv 文件在 .csv 之前包含一个 .jsonl 扩展名(思考 file.jsonl.csv
)关于如何在后面添加 csv 扩展名之前删除 .jsonl 扩展名的任何想法?我希望我能够摆脱 csv 文件的 .jsonl 扩展名,因为它将来可能会造成混淆。谢谢!
创建的示例 CSV 文件:
20210531_CCXT_FTX_DOGEPERP.jsonl.csv
我的脚本:
import glob
import json
import csv
import time
start = time.time()
#import pandas as pd
from flatten_json import flatten
#Path of jsonl file
File_path = (r'C:\Users\Natthanon\Documents\Coding 101\Python\JSONL')
#reading all jsonl files
files = [f for f in glob.glob( File_path + "**/*.jsonl", recursive=True)]
i = 0
for f in files:
with open(f, 'r') as F:
#creating csv files
with open(r'C:\Users\Natthanon\Documents\Coding 101\Python\CSV\' + f.split("\")[-1] + ".csv", 'w' , newline='') as csv_file:
thewriter = csv.writer(csv_file)
thewriter.writerow(["symbol", "timestamp", "datetime","high","low","bid","bidVolume","ask","askVolume","vwap","open","close","last","previousClose","change","percentage","average","baseVolume","quoteVolume"])
for line in F:
#flatten json files
data = json.loads(line)
data_1 = flatten(data)
#headers should be the Key values from json files that make Column header
thewriter.writerow([data_1['symbol'],data_1['timestamp'],data_1['datetime'],data_1['high'],data_1['low'],data_1['bid'],data_1['bidVolume'],data_1['ask'],data_1['askVolume'],data_1['vwap'],data_1['open'],data_1['close'],data_1['last'],data_1['previousClose'],data_1['change'],data_1['percentage'],data_1['average'],data_1['baseVolume'],data_1['quoteVolume']])
问题是因为您在写入新文件时没有去掉扩展名,用这样的东西来代替您创建的 csv 文件应该可以解决这个问题
file_name = f.rsplit("\", 1)[-1].replace('.jsonl', '')
with open(r'C:\Users\Natthanon\Documents\Coding 101\Python\CSV\' + file_name + ".csv", 'w' , newline='') as csv_file: