json2xls icon indicating copy to clipboard operation
json2xls copied to clipboard

more than 256 columns error

Open Lubyam opened this issue 4 years ago • 3 comments

ValueError: column index (256) not an int in range(256)

Lubyam avatar Jan 29 '21 08:01 Lubyam

ValueError: column index (256) not an int in range(256)

How to reproduce this error?

axiaoxin avatar Jan 29 '21 08:01 axiaoxin

File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/json2xls/json2xls.py", line 210, in flatten
    _flatten(data_dict, parent_key, sep)            File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/json2xls/json2xls.py", line 197, in _flatten                                         if isinstance(x, collections.MutableMapping):                      ^^^^^^^^^^^^^^^^^^^^^^^^^^   AttributeError: module 'collections' has no attribute 'MutableMapping' 

etc can be fixed with:

sed -i 's/collections.MutableMapping/collections.abc.MutableMapping/g' /data/data/com.termux/files/usr/lib/python3.11/site-packages/json2xls/json2xls.py

sed -i 's/collections.MutableSequence/collections.abc.MutableSequence/g' /data/data/com.termux/files/usr/lib/python3.11/site-packages/json2xls/json2xls.py

But then , same one as above. Ms Bing explains it as :

I see that you're now encountering a different error: ValueError: column index (256) not an int in range(256). This error is occurring because the Excel file format (.xls) only supports up to 256 columns, and your data appears to have more than 256 keys (which would be converted to columns).

Unfortunately, without modifying the json2xls package or using a different package that supports more columns, there's no straightforward way to fix this error in Python code. I recommend reaching out to the maintainer of the json2xls package for assistance, or considering alternative methods for converting your JSON data to Excel format.

Manamama avatar Apr 20 '24 14:04 Manamama

And a quick tip from Ms Bing:

'openpyxl' is needed , plus pandas, and a version of:

import pandas as pd
import json

# Load raw data from JSON file
with open('/storage/emulated/0/Download/freedom.json', 'r') as f:
    raw_data = json.load(f)

# Preprocess raw data to create a new JSON object
data = []
for segment in raw_data['segments']:
    for word in segment['words']:
        # Extract data from nested objects and arrays
        data.append({
            'segment_start': segment['start'],
            'segment_end': segment['end'],
            'word_start': word['start'],
            'word_end': word['end'],
            'word': word['word'],
            'score': word['score'],
        })

# Convert preprocessed data to DataFrame
df = pd.DataFrame(data)

# Convert DataFrame to Excel
df.to_excel('/storage/emulated/0/Download/freedom.json.xlsx', index=False)

Manamama avatar Apr 20 '24 14:04 Manamama