填充表格数据的打印输出 [英] Padding printed output of tabular data

查看:185
本文介绍了填充表格数据的打印输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这可能是死的简单,但我在一个文件中有这样的一些数据:

 朝鲜蓟

绿色地球仪,帝国之星,紫罗兰色

24深

豆,利马

布什宝贝,布什利马,Fordhook,Fordhook 242

12宽x 8-10深

我希望能够格式化成一个很好的TSV类型的表,看起来像这样:

 名称|品种|容器数据
---------- | ------------- | - -----
这里有些数据很好地填充了间距和右对齐的文本


解决方案

这是一个相当完整的例子,假设如下


  • 产品列表包含在名为veg.txt的文件

  • 您的数据安排在每条记录的三行中,其中包含字段o n连续行



我有点小菜鸟,所以无疑有更好更优雅的方法来做到这一点

 #!/ usr / bin / ruby​​ 

class Vegetable

@@ max_name || = 0
@@ max_variety || = 0
@@ max_container || = 0

attr_reader:name,:variety,:container

初始化(名称,种类,容器)
@name = name
@variety = variety
@container = container

@@ max_name = set_max(@name .length,@@ max_name)
@@ max_variety = set_max(@ variety.length,@@ max_variety)
@@ max_container = set_max(@ container.length,@@ max_container)
end

def set_max(current,max)
current>最大? current:max
end
$ b def self.max_name
@@ max_name
end
$ b $ def self.max_variety
@@ max_variety
end

def self.max_container()
@@ max_container
end

end

产品= []


File.open(veg.txt)do |文件|

while name = file.gets
name = name.strip
variety = file.gets.to_s.strip
container = file.gets.to_s.strip
veg = Vegetable.new(名称,品种,容器)
产品<< veg
end
end

format =%#{Vegetable.max_name} s\t%#{Vegetable.max_variety} s\t%#{Vegetable.max_container (格式,名称,品种,容器)
printf(格式,----,------- ,---------)
products.each do | p |
printf(格式,p.name,p.variety,p.container)
结尾

以下示例文件

 
朝鲜蓟
绿色地球,帝国之星,紫罗兰色
24深
豆类,利马
布什宝贝,布什利马,福特钩,福特钩242
12宽x 8-10深
土豆
爱德华王,泽西,泽西皇家
36宽x 8-10深

产生以下输出

 
名称种类容器
---- ------- ---------
朝鲜蓟绿色地球,帝国之星,紫罗兰色24 深
豆,利马布什宝贝,布什利马,Fordhook,Fordhook 242 12宽x 8-10深
土豆爱德华王,Desiree,泽西皇家36宽x 8-10深


I know this is probably dead simple, but I've got some data such as this in one file:

Artichoke

Green Globe, Imperial Star, Violetto

24" deep

Beans, Lima

Bush Baby, Bush Lima, Fordhook, Fordhook 242

12" wide x 8-10" deep

that I'd like to be able to format into a nice TSV type of table, to look something like this:

    Name  | Varieties    | Container Data
----------|------------- |-------
some data here nicely padded with even spacing and right aligned text 

解决方案

This is a reasonably full example that assumes the following

  • Your list of products is contained in a file called veg.txt
  • Your data is arranged across three lines per record with the fields on consecutive lines

I am a bit of a noob to rails so there are undoubtedly better and more elegant ways to do this

#!/usr/bin/ruby

class Vegetable

  @@max_name ||= 0  
  @@max_variety ||= 0  
  @@max_container ||= 0  

  attr_reader :name, :variety, :container

  def initialize(name, variety, container)
    @name = name
    @variety = variety
    @container = container  

    @@max_name = set_max(@name.length, @@max_name)  
    @@max_variety = set_max(@variety.length, @@max_variety)  
    @@max_container = set_max(@container.length, @@max_container)
  end

  def set_max(current, max)
    current > max ? current : max
  end

  def self.max_name  
    @@max_name  
  end  

  def self.max_variety  
    @@max_variety  
  end  

  def self.max_container()  
    @@max_container  
  end  

end

    products = []


    File.open("veg.txt") do | file|

      while name = file.gets
        name = name.strip
        variety = file.gets.to_s.strip
        container = file.gets.to_s.strip
        veg = Vegetable.new(name, variety, container)
        products << veg
      end
    end

    format="%#{Vegetable.max_name}s\t%#{Vegetable.max_variety}s\t%#{Vegetable.max_container}s\n"
    printf(format, "Name", "Variety", "Container")
    printf(format, "----", "-------", "---------")
    products.each do |p|
        printf(format, p.name, p.variety, p.container)
    end

The following sample file

Artichoke
Green Globe, Imperial Star, Violetto
24" deep
Beans, Lima
Bush Baby, Bush Lima, Fordhook, Fordhook 242
12" wide x 8-10" deep
Potatoes
King Edward, Desiree, Jersey Royal
36" wide x 8-10" deep

Produced the following output

       Name                                      Variety                Container
       ----                                      -------                ---------
  Artichoke         Green Globe, Imperial Star, Violetto                 24" deep
Beans, Lima Bush Baby, Bush Lima, Fordhook, Fordhook 242    12" wide x 8-10" deep
   Potatoes           King Edward, Desiree, Jersey Royal    36" wide x 8-10" deep

这篇关于填充表格数据的打印输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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