Потеря исходного текста при выполнении tostring () - PullRequest
1 голос
/ 24 февраля 2020

У меня есть json объект с одним из ключей:

"transcript": "The universe is bustling with matter and energy. Even in the vast apparent emptiness of intergalactic space, there's one hydrogen atom per cubic meter. That's not the mention a barrage of particles and electromagnetic radiation passing every which way from stars, galaxies, and into black holes. There's even radiation left over from the Big Bang...

Загрузка фрейма данных:

#initialize dataframe for the universe transcript 

dfJson = pd.read_json('test1.json')

Вот мой код для попытки извлечь его.

dfJsonTranscript = dfJson.get('transcript').to_string()
pprint.pprint(dfJsonTranscript)

text_file = open("sample.txt", "wt")
n = text_file.write(dfJsonTranscript)
text_file.close()

Мой вывод

0      The universe is bustling with matter and energ...
1      The universe is bustling with matter and energ...
2      The universe is bustling with matter and energ...
3      The universe is bustling with matter and energ...
4      The universe is bustling with matter and energ...
5      The universe is bustling with matter and energ...
6      The universe is bustling with matter and energ...
7      The universe is bustling with matter and energ...
8      The universe is bustling with matter and energ...

Оригинал JSON:

"transcript": "The universe is bustling with matter and energy. Even in the vast apparent emptiness of intergalactic space, there's one hydrogen atom per cubic meter. That's not the mention a barrage of particles and electromagnetic radiation passing every which way from stars, galaxies, and into black holes. There's even radiation left over from the Big Bang... universe. ",
  "words": [
    {
      "alignedWord": "the",
      "case": "success",
      "end": 6.31,
      "endOffset": 3,
      "phones": [
        {
          "duration": 0.09,
          "phone": "dh_B"
        },
        {
          "duration": 0.05,
          "phone": "iy_E"
        }
      ],
      "start": 6.17,
      "startOffset": 0,
      "word": "The"
    },
    {
      "alignedWord": "universe",
      "case": "success",
      "end": 6.83,
      "endOffset": 12,
      "phones": [
        {
          "duration": 0.08,
          "phone": "y_B"
        },

Почему я теряю исходное значение моего ключа при запуске toString ( ) метод на это. Я теряю это, потому что я превращаю это в массив данных через pandas?

1 Ответ

0 голосов
/ 24 февраля 2020

Попробуйте это:

dfJsonTranscript = dfJson.get('transcript').to_string(index=False)

Установка index=False Мы можем указать to_string способ DataFrame не печатать индексные (строки) метки.

Редактировать: Чтобы предотвратить усечение строки, вы можете установить свойство max_colwidth в pandas, это необходимо установить перед вызовом to_string метода.

pd.set_option("display.max_colwidth", None)

ОБНОВЛЕНИЕ:

#initialize dataframe for the universe transcript 
import pandas as pd
import pprint

pd.set_option('display.max_colwidth', None) #--> To avoid truncation

dfJson = pd.read_json('data.json')
dfJsonTranscript = dfJson.get('transcript').to_string(index=False).strip()
pprint.pprint(dfJsonTranscript)

text_file = open("sample.txt", "wt")
n = text_file.write(dfJsonTranscript)
text_file.close()
...