• <small id='kNAKC'></small><noframes id='kNAKC'>

    <tfoot id='kNAKC'></tfoot>

      <legend id='kNAKC'><style id='kNAKC'><dir id='kNAKC'><q id='kNAKC'></q></dir></style></legend>
      <i id='kNAKC'><tr id='kNAKC'><dt id='kNAKC'><q id='kNAKC'><span id='kNAKC'><b id='kNAKC'><form id='kNAKC'><ins id='kNAKC'></ins><ul id='kNAKC'></ul><sub id='kNAKC'></sub></form><legend id='kNAKC'></legend><bdo id='kNAKC'><pre id='kNAKC'><center id='kNAKC'></center></pre></bdo></b><th id='kNAKC'></th></span></q></dt></tr></i><div id='kNAKC'><tfoot id='kNAKC'></tfoot><dl id='kNAKC'><fieldset id='kNAKC'></fieldset></dl></div>
          <bdo id='kNAKC'></bdo><ul id='kNAKC'></ul>
      1. Python - 在对齐的列中打印 CSV 字符串列表

        时间:2023-10-09
          <i id='WO60s'><tr id='WO60s'><dt id='WO60s'><q id='WO60s'><span id='WO60s'><b id='WO60s'><form id='WO60s'><ins id='WO60s'></ins><ul id='WO60s'></ul><sub id='WO60s'></sub></form><legend id='WO60s'></legend><bdo id='WO60s'><pre id='WO60s'><center id='WO60s'></center></pre></bdo></b><th id='WO60s'></th></span></q></dt></tr></i><div id='WO60s'><tfoot id='WO60s'></tfoot><dl id='WO60s'><fieldset id='WO60s'></fieldset></dl></div>
          <legend id='WO60s'><style id='WO60s'><dir id='WO60s'><q id='WO60s'></q></dir></style></legend>
        1. <small id='WO60s'></small><noframes id='WO60s'>

            <tbody id='WO60s'></tbody>

              <tfoot id='WO60s'></tfoot>
                  <bdo id='WO60s'></bdo><ul id='WO60s'></ul>
                  本文介绍了Python - 在对齐的列中打印 CSV 字符串列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我编写了一个完全兼容 Python 2Python 3 的代码片段.我编写的片段解析数据并将输出构建为 CSV 字符串列表.

                  I have written a fragment of code that is fully compatible with both Python 2 and Python 3. The fragment that I wrote parses data and it builds the output as a list of CSV strings.

                  脚本提供了一个选项来:

                  • 将数据写入 CSV 文件,或
                  • 将其显示到 stdout.

                  虽然在显示到 stdout(第二个项目符号选项)时,我可以轻松地遍历列表并将 , 替换为 ,但这些项目长度是任意的,因此由于制表符的差异,请不要以很好的格式排列.

                  While I could easily iterate through the list and replace , with when displaying to stdout (second bullet option), the items are of arbitrary length, so don't line up in a nice format due to variances in tabs.

                  我做了很多研究,我相信字符串格式选项可以完成我所追求的.也就是说,我似乎找不到可以帮助我正确使用语法的示例.

                  I have done quite a bit of research, and I believe that string format options could accomplish what I'm after. That said, I can't seem to find an example that helps me get the syntax correct.

                  我宁愿不使用外部库.我知道如果我走这条路有很多可用的选项,但我希望脚本尽可能兼容和简单.

                  I would prefer to not use an external library. I am aware that there are many options available if I went that route, but I want the script to be as compatible and simple as possible.

                  这是一个例子:

                  value1,somevalue2,value3,reallylongvalue4,value5,superlongvalue6
                  value1,value2,reallylongvalue3,value4,value5,somevalue6
                  

                  你能帮帮我吗?任何建议将不胜感激.

                  Can you help me please? Any suggestion will be much appreciated.

                  推荐答案

                  import csv
                  from StringIO import StringIO
                  
                  rows = list(csv.reader(StringIO(
                      '''value1,somevalue2,value3,reallylongvalue4,value5,superlongvalue6
                  value1,value2,reallylongvalue3,value4,value5,somevalue6''')))
                  
                  widths = [max(len(row[i]) for row in rows) for i in range(len(rows[0]))]
                  
                  for row in rows:
                      print(' | '.join(cell.ljust(width) for cell, width in zip(row, widths)))
                  

                  输出:

                  value1 | somevalue2 | value3           | reallylongvalue4 | value5 | superlongvalue6
                  value1 | value2     | reallylongvalue3 | value4           | value5 | somevalue6     
                  

                  这篇关于Python - 在对齐的列中打印 CSV 字符串列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在python中用波兰语中的月份名称格式化日期 下一篇:Python格式字符串中的宽度变量

                  相关文章

                  最新文章

                • <legend id='C4WaZ'><style id='C4WaZ'><dir id='C4WaZ'><q id='C4WaZ'></q></dir></style></legend>
                  <tfoot id='C4WaZ'></tfoot>
                    <bdo id='C4WaZ'></bdo><ul id='C4WaZ'></ul>
                  1. <i id='C4WaZ'><tr id='C4WaZ'><dt id='C4WaZ'><q id='C4WaZ'><span id='C4WaZ'><b id='C4WaZ'><form id='C4WaZ'><ins id='C4WaZ'></ins><ul id='C4WaZ'></ul><sub id='C4WaZ'></sub></form><legend id='C4WaZ'></legend><bdo id='C4WaZ'><pre id='C4WaZ'><center id='C4WaZ'></center></pre></bdo></b><th id='C4WaZ'></th></span></q></dt></tr></i><div id='C4WaZ'><tfoot id='C4WaZ'></tfoot><dl id='C4WaZ'><fieldset id='C4WaZ'></fieldset></dl></div>

                    <small id='C4WaZ'></small><noframes id='C4WaZ'>