将文本文件转换为表格格式 [英] Turning a text file into a tabular format

查看:107
本文介绍了将文本文件转换为表格格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试正确格式化文本文件以符合学校项目所需的标准时遇到问题.我已经坚持了一段时间,我对编码仍然很陌生,想知道是否有人有我可以理解和实施的答案,希望我可以向更有经验的人学习.

I'm having issues trying to properly format a text file to fit the needed criteria for a school project. I've been stuck on it for a while and I'm still very new to coding and wanted to know if anyone has an answer that I can understand and implement, hopefully I can learn from those much more experienced.

我想转换一个用户可以输入的文本文件,在文件中看起来像这样:

I want to convert a text file that can be entered by a user that looks like this within the file:

Lennon 12 3.33
McCartney 57 7
Harrison 11 9.1
Starr 3 4.13

并创建它以适合这样的表格格式:

and create it to fit a tabular format like this:

Name            Hours      Total Pay
Lambert            34         357.00
Osborne            22         137.50
Giacometti          5         503.50 

我可以创建标题,虽然它可能不是很漂亮的代码,但是当我打印测试文件的内容时,它通常是这样的:

I can create the headers, though it may not be pretty code, but when I print the contents of the test file it usually turns out like this:

Name            Hour      Total pay
Lennon 12 3.33
McCartney 57 7
Harrison 11 9.1
Starr 3 4.13

而且我不明白如何将其正确格式化以使其看起来像一个适当的表格,该表格正确合理且与实际标题正确一致,我不确定如何真正解决它或什至从哪里开始,因为我没有没有在这方面提出任何实际依据.

And I don't understand how to properly format it to look like a proper table that's right justified and properly in line with the actual headers, I'm not sure how to really tackle it or where to even start as I haven't made any real ground on this.

在尝试使用诸如 file_open.read().rstrip("\n).format 之类的东西后,我已经将我的代码分解为骨架> 把索引弄得一团糟,有时以某种方式最终只出现一个字母:

I've gutted my code and broke it down into just the skeleton after trying with things like file_open.read().rstrip("\n) and .format making a mess of the indexes and sometimes somehow ending up with only single letters to appear:

file_name = input("Enter the file name: ")

print("Name" + " " * 12 + "Hour" + " " * 6 + "Total pay")
with open(file_name, 'r') as f:

    for line in f:

        print(line, end='')

我知道它看起来很简单,因为它确实如此.我们的讲师希望我们使用打开"命令,并尽量远离那些可能会降低可读性但仍然尽可能紧凑的东西.这包括导入第三方工具,这些工具降低了使用beautifultable之类的东西的机会,就像其他一些朋友提供的那样更简单.

I know it looks simple, and because it is. Our instructor wanted us to work with the "open" command and try and stay away from things that could make it less readable but still as compact as possible. This includes the importing of third party tools which shot down chances to use things like beautifultable like a few other friends have offered as an easier way out.

我有一个同学说要阅读将其转换为列表的行,然后使用一些格式对其进行调整,另一个同学说我可能可以在不列出的情况下对其进行格式化;虽然我发现如果把它变成一个列表,换行符\n"出现在每个列表索引的末尾

I had a classmate say to read the lines that turns it into a list and adjust it from there with some formatting, and another classmate said I could probably format it without listing it; although I found that the newline character "\n" appears at the end of each list index if turning it into a list

例如:['Lennon 12 3.33\n', 'McCartney 57 7\n', 'Harrison 11 9.1\n', 'Starr 3 4.13']

虽然我不明白如何格式化列表中的内容,以便名称可以与每个数字变量分开并与标题一致,因为我对<没有太多经验em>for 循环,很多人说如果我有这种想法,可以在我的班级中轻松解决.

Though what I don't understand is how to format the things that are within the list so that the name can be separated from each of the number variables and in line with the header as I don't have much experience with for loops that many say can be an easy fix within my class if I have that down pat.

我并不是在寻找直接编码的答案,而是在寻找正确方向的点或在哪里阅读有关如何操作所列内容的信息

I'm not exactly looking for straight coded answers, but rather a point in the right direction or where to read up on how to manipulate listed content

推荐答案

这里有一些东西可以帮助您朝着正确的方向前进:

Here's something to get you headed in the right direction:

data_filename = 'employees.txt'
headers = 'Name', 'Hours', 'Rate'  # Column names.

# Read the data from file into a list-of-lists table.
with open(data_filename) as file:
    datatable = [line.split() for line in file.read().splitlines()]

# Find the longest data value or header to be printed in each column.
widths = [max(len(value) for value in col)
            for col in zip(*(datatable + [headers]))]

# Print heading followed by the data in datatable.
# (Uses '>' to right-justify the data in some columns.)
format_spec = '{:{widths[0]}}  {:>{widths[1]}}  {:>{widths[2]}}'
print(format_spec.format(*headers, widths=widths))
for fields in datatable:
    print(format_spec.format(*fields, widths=widths))

输出:

Name       Hours  Rate
Lennon        12  3.33
McCartney     57     7
Harrison      11   9.1
Starr          3  4.13

这篇关于将文本文件转换为表格格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆