Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

926
Views
python & json.dump: cómo hacer una matriz interna en una línea

Mis códigos de Python:

 with open('outputFile.json', 'w') as outfile: json.dump(ans, outfile, indent=4, separators=(',', ': '))

El archivo de salida es

 [ { "rowLength": 5, "alphabet": [ "Q", "W", "I", "B", "P", "A", "S" ] }, { "rowLength": 3, "alphabet": [ "S", "D", "E", "U", "I", "O", "L" ] } ]

¿Cómo convertir la matriz interna en una sola línea? Gracias

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Creo que esto podría ser propenso a errores si el formato de la salida cambiara alguna vez, pero ¿solo como una idea?

 >>> d = [{'rowLength': 5, 'alphabet': ['Q', 'W', 'I', 'B', 'P', 'A', 'S']}, {'rowLength': 3, 'alphabet': ['S', 'D', 'E', 'U', 'I', 'O', 'L']}] >>> import json >>> output = json.dumps(d, indent=4) >>> import re >>> print(re.sub(r'",\s+', '", ', output)) [ { "rowLength": 5, "alphabet": [ "Q", "W", "I", "B", "P", "A", "S" ] }, { "rowLength": 3, "alphabet": [ "S", "D", "E", "U", "I", "O", "L" ] } ]

O con múltiples reemplazos (algo como esto sería mejor) :

 >>> output = json.dumps(d, indent=4) >>> output2 = re.sub(r'": \[\s+', '": [', output) >>> output3 = re.sub(r'",\s+', '", ', output2) >>> output4 = re.sub(r'"\s+\]', '"]', output3) >>> print(output4) [ { "rowLength": 5, "alphabet": ["Q", "W", "I", "B", "P", "A", "S"] }, { "rowLength": 3, "alphabet": ["S", "D", "E", "U", "I", "O", "L"] } ]
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!