@Speakermen

Зачем при конвертации csv в json теряется первая строка?

Всего в csv 292 строки на выходе 291 первая строка теряется но почему это происходит? Думаю я делаю что-то не так в readCsv
def readCsv(option, search):
    df = pd.read_csv(f'{option}.csv')
    df.columns = ["name", "image", "link", "price", "discount"]
    res = df[df.name.str.contains(
        search, regex=True, flags=re.IGNORECASE)]
    res['name'] = df['name'].str.partition('[')[0]
    return res.sort_values(by='price', ascending=True)


@require_http_methods(["GET"])
def motherboard(request):
    data = json.loads(readCsv('motherboard', '.').to_json(orient='records'))
    return render(request, 'home/index.html', {"data": data})


@require_http_methods(["GET"])
def modelCpu(request, model):
    if (whiteList(model)):
        data = json.loads(readCsv('cpu', whiteList(model)
                                  ).to_json(orient='records'))
        return render(request, 'home/index.html', {"data": data})

    return HttpResponseNotFound("<h1>Page not found</h1>")
  • Вопрос задан
  • 74 просмотра
Решения вопроса 2
vabka
@vabka
Токсичный шарпист
Видимо есть настройка, которая убирает заголовок
Ответ написан
Предполагается, что в первой строке CSV-файла идёт заголовок с именами колонок.
header: int, list of int, None, default ‘infer’
Row number(s) to use as the column names, and the start of the data. Default behavior is to infer the column names: if no names are passed the behavior is identical to header=0 and column names are inferred from the first line of the file, if column names are passed explicitly then the behavior is identical to header=None. Explicitly pass header=0 to be able to replace existing names. The header can be a list of integers that specify row locations for a multi-index on the columns e.g. [0,1,3]. Intervening rows that are not specified will be skipped (e.g. 2 in this example is skipped). Note that this parameter ignores commented lines and empty lines if skip_blank_lines=True, so header=0 denotes the first line of data rather than the first line of the file.
https://pandas.pydata.org/docs/reference/api/panda...
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы