如何使用Python而不是任何外部工具漂亮地打印CSV文件?
例如,我有这个CSV文件:
title1|title2|title3|title4
datalongdata|datalongdata|data|data
data|data|data|datalongdatadatalongdatadatalongdatadatalongdatadatalongdata
data|data'data|dat
我想把它变成一张桌子。例如,对这样的事情:
+ --------------------------------------------------------------------------------------------------- +
| title1 | title2 | title3 | title4 |
+ --------------------------------------------------------------------------------------------------- +
| datalongdata | datalongdata | data | data |
| | | | |
| data | data | data | datalongdatadatalongdatadatalongdatadatalongdatadatalongdata |
| data | data'data | dat | |
+ --------------------------------------------------------------------------------------------------- +
用法:
漂亮的.pretty_文件(文件名,***选项*)
读取CSV文件并将数据作为表直观地打印到新文件中。filename是给定的CSV文件。可选的***options*关键字参数是Python的标准库csv模块Dialects and Formatting Parameters和以下列表的联合:
示例:
Python3:
这是一个Python 2实现。对于Python 3,由于python3中的
csv.reader
希望以文本模式打开文件,因此必须将open(filename, "rb") as input:
行的两次出现改为open(filename, "r", newline="") as input:
。模块:
相关问题 更多 >
编程相关推荐