Python 在将 JsonL 转换为 CSV 文件时删除 .jsonl 扩展名

问题描述

我有一个脚本,可以将选定目录中的 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: