【问题标题】:Python removing .jsonl extension when converting JsonL to CSV FilePython 在将 JsonL 转换为 CSV 文件时删除 .jsonl 扩展名
【发布时间】:2021-08-24 00:42:49
【问题描述】:

我有一个脚本,可以将选定目录中的 jsonl 文件转换为另一个指定位置的 csv 文件。但是,在将文件转换为 csv 格式后,最终创建的 csv 文件在 .csv 之前包含一个 .jsonl 扩展名(想想file.jsonl.csv)关于如何在后面添加 csv 扩展名之前删除 .jsonl 扩展名的任何想法?我希望我能够摆脱 .jsonl csv 文件的扩展名,因为它将来可能会令人困惑。谢谢!

创建的示例 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']])

【问题讨论】:

    标签: python json csv type-conversion jsonlines


    【解决方案1】:

    问题是因为您在写入新文件时没有摆脱扩展名,像这样替换您创建的 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:
    

    【讨论】:

      猜你喜欢
      • 2021-07-29
      • 2016-05-14
      • 2021-08-21
      • 2021-07-05
      • 2021-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多