如何重新格式化已获取的psql查询输出?

huangapple go评论94阅读模式
英文:

How can I reformat already acquired psql query output?

问题

我有默认的psql查询输出保存在.txt文件中,很难阅读。有没有任何命令或服务可以将其转换成更方便处理的格式,比如CSV?

查看了文档,但没有找到解决方案。有没有可能以交互模式在psql中打开它,就像我自己查询了一样?

英文:

I have default psql query output in .txt file, which is very difficult to read. Any command or service to change it into a more convenient format to work with (e.g. CSV)?

Went through docs, but didn't find the solution. By any chance, can I somehow open it with psql in interactive mode, as if I queried it myself?

答案1

得分: 1

我已经为此编写了Python脚本,没有找到更好的解决方案。

  1. import sys
  2. def convert_to_csv(input_file, output_file):
  3. with open(input_file, 'r') as inp, open(output_file, 'w') as outp:
  4. lines = inp.readlines()
  5. # 处理标题
  6. buffer = ''
  7. i = 1 # 跳过查询行
  8. while not lines[i].startswith('---'):
  9. buffer += lines[i].rstrip()
  10. i += 1
  11. header = buffer.split('|')
  12. header = [h.strip() for h in header]
  13. num_of_columns = len(header)
  14. outp.write(','.join(header) + '\n')
  15. # 跳过分隔线
  16. while lines[i].startswith('---'):
  17. i += 1
  18. # 处理数据行
  19. buffer = ''
  20. for line in lines[i:-1]: # 跳过最后一行(行数)
  21. buffer += line.rstrip()
  22. if buffer.count('|') == num_of_columns - 1:
  23. row = buffer.split('|')
  24. row = [f'"{value.strip()}"' if ',' in value.strip() else value.strip() for value in row]
  25. outp.write(','.join(row) + '\n')
  26. buffer = ''
  27. if buffer: # 确保最后一部分数据被写入
  28. row = buffer.split('|')
  29. row = [f'"{value.strip()}"' if ',' in value.strip() else value.strip() for value in row]
  30. outp.write(','.join(row) + '\n')
  31. convert_to_csv(sys.argv[1], sys.argv[2])

在终端中运行它:python3 psql_to_csv.py exp1.txt outp1.csv。输入文件应该以查询命令开头,如db_name=# select * ...,并以(N行)结尾。

英文:

I have written python script for that, haven't find no better solution.

  1. import sys
  2. def convert_to_csv(input_file, output_file):
  3. with open(input_file, 'r') as inp, open(output_file, 'w') as outp:
  4. lines = inp.readlines()
  5. # Process header
  6. buffer = ''
  7. i = 1 # Skip query line
  8. while not lines[i].startswith('---'):
  9. buffer += lines[i].rstrip()
  10. i += 1
  11. header = buffer.split('|')
  12. header = [h.strip() for h in header]
  13. num_of_columns = len(header)
  14. outp.write(','.join(header) + '\n')
  15. # Skip separator line(s)
  16. while lines[i].startswith('---'):
  17. i += 1
  18. # Process data rows
  19. buffer = ''
  20. for line in lines[i:-1]: # Skip last line (row count)
  21. buffer += line.rstrip()
  22. if buffer.count('|') == num_of_columns - 1:
  23. row = buffer.split('|')
  24. row = [f'"{value.strip()}"' if ',' in value.strip() else value.strip() for value in row]
  25. outp.write(','.join(row) + '\n')
  26. buffer = ''
  27. if buffer: # Ensure last bit of data is written
  28. row = buffer.split('|')
  29. row = [f'"{value.strip()}"' if ',' in value.strip() else value.strip() for value in row]
  30. outp.write(','.join(row) + '\n')
  31. convert_to_csv(sys.argv[1], sys.argv[2])

Run it in terminal python3 psql_to_csv.py exp1.txt outp1.csv. Input file should start with query command like db_name=# select * ... and end with (N rows)

huangapple
  • 本文由 发表于 2023年5月26日 16:38:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76339094.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定