我导入的 .txt table 没有与 headers (Python) 正确对齐
My imported .txt table does not line up correctly with the headers (Python)
所以基本上我必须导入一个预设 .txt 文件并且一切正常,除了一件小事,即代码中的预设 headers 与 .txt 文件数据不一致正确。
import csv
from tabulate import tabulate as tb
F = input("Enter the name of the file that you would like to import:")
fields = list(csv.reader(open(F + '.txt', 'r'), delimiter=';'))
print(tb(fields, headers=['Company', 'Registration number.', 'Date', 'Adress', 'Telephone Number']))
然后代码打印出这个。
Company Registration number. Date Adress Telephone Number
------ --------- ---------------------- ----------------------------- ----------- ------------------
Valve 60006672 03.13.2003. Brown street, Athens, Greece. 14223157963
Google 50003495 10.24.2001. Lenin street, Moscow, Russia. 53221745750
Apple 20000196 03.31.2008. Second street, New York, USA. 55327537161
.txt文件中的信息:
阀门; 60006672;03.13.2003.;Brown street, Athens, Greece..;14223157963;
Google;50003495;10.24.2001.;列宁街,莫斯科,俄罗斯。;53221745750;
Apple;20000196;03.31.2008.;美国纽约第二街;55327537161;
Question: Table output does not line up correctly with the headers?
每一行中的最后一个 ;
指向 一个 列。
Valve; 60006672;03.13.2003.;Brown street, Athens, Greece..;14223157963;
因此您的 headers=
的计数少了 一个。
Solution:
从每一行中删除最后一个 ';'
或将空的 ''
添加到 headers=
tb(fields,
headers=['Company', 'Registration\nNumber', 'Date',
'Adress', 'Telephone\nNumber', '']
)
Output:
Company Registration Date Adress Telephone
Number Number
--------- ----------- ----------- ------------------------------ ----------- --
Valve 60006672 03.13.2003. Brown street, Athens, Greece.. 14223157963
Google 50003495 10.24.2001. Lenin street, Moscow, Russia. 53221745750
Apple 20000196 03.31.2008. Second street, New York, USA. 55327537161
所以基本上我必须导入一个预设 .txt 文件并且一切正常,除了一件小事,即代码中的预设 headers 与 .txt 文件数据不一致正确。
import csv
from tabulate import tabulate as tb
F = input("Enter the name of the file that you would like to import:")
fields = list(csv.reader(open(F + '.txt', 'r'), delimiter=';'))
print(tb(fields, headers=['Company', 'Registration number.', 'Date', 'Adress', 'Telephone Number']))
然后代码打印出这个。
Company Registration number. Date Adress Telephone Number
------ --------- ---------------------- ----------------------------- ----------- ------------------
Valve 60006672 03.13.2003. Brown street, Athens, Greece. 14223157963
Google 50003495 10.24.2001. Lenin street, Moscow, Russia. 53221745750
Apple 20000196 03.31.2008. Second street, New York, USA. 55327537161
.txt文件中的信息:
阀门; 60006672;03.13.2003.;Brown street, Athens, Greece..;14223157963; Google;50003495;10.24.2001.;列宁街,莫斯科,俄罗斯。;53221745750; Apple;20000196;03.31.2008.;美国纽约第二街;55327537161;
Question: Table output does not line up correctly with the headers?
每一行中的最后一个 ;
指向 一个 列。
Valve; 60006672;03.13.2003.;Brown street, Athens, Greece..;14223157963;
因此您的 headers=
的计数少了 一个。
Solution:
从每一行中删除最后一个 ';'
或将空的 ''
添加到 headers=
tb(fields,
headers=['Company', 'Registration\nNumber', 'Date',
'Adress', 'Telephone\nNumber', '']
)
Output:
Company Registration Date Adress Telephone
Number Number
--------- ----------- ----------- ------------------------------ ----------- --
Valve 60006672 03.13.2003. Brown street, Athens, Greece.. 14223157963
Google 50003495 10.24.2001. Lenin street, Moscow, Russia. 53221745750
Apple 20000196 03.31.2008. Second street, New York, USA. 55327537161