免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
1234
最近访问板块 发新帖
打印 上一主题 下一主题

TXT中一个坐标列,一个数据列,将数据按坐标生成map [复制链接]

论坛徽章:
0
31 [报告]
发表于 2018-02-02 19:11 |只看该作者
回复 30# 523066680

19楼

论坛徽章:
12
子鼠
日期:2014-10-11 16:46:482016科比退役纪念章
日期:2018-03-16 10:24:0515-16赛季CBA联赛之山东
日期:2017-11-10 14:32:142016科比退役纪念章
日期:2017-09-02 15:42:4715-16赛季CBA联赛之佛山
日期:2017-08-28 17:11:5515-16赛季CBA联赛之浙江
日期:2017-08-24 16:55:1715-16赛季CBA联赛之青岛
日期:2017-08-17 19:55:2415-16赛季CBA联赛之天津
日期:2017-06-29 10:34:4315-16赛季CBA联赛之四川
日期:2017-05-16 16:38:55黑曼巴
日期:2016-07-19 15:03:112015亚冠之萨济拖拉机
日期:2015-05-22 11:38:5315-16赛季CBA联赛之北京
日期:2019-08-13 17:30:53
32 [报告]
发表于 2018-02-02 19:23 |只看该作者
回复 31# Okelani

原来是这个问题,都怪 CU论坛。

论坛徽章:
30
15-16赛季CBA联赛之八一
日期:2020-10-27 13:24:2315-16赛季CBA联赛之山东
日期:2020-10-27 13:24:07
33 [报告]
发表于 2018-02-03 10:46 |只看该作者
回复 30# 523066680

唉,看到你们“志同道合”的样子 羡慕嫉妒恨啊!!
扶我扶我就扶我~map出来了。我来试试坐标吧。

给跪!

论坛徽章:
12
子鼠
日期:2014-10-11 16:46:482016科比退役纪念章
日期:2018-03-16 10:24:0515-16赛季CBA联赛之山东
日期:2017-11-10 14:32:142016科比退役纪念章
日期:2017-09-02 15:42:4715-16赛季CBA联赛之佛山
日期:2017-08-28 17:11:5515-16赛季CBA联赛之浙江
日期:2017-08-24 16:55:1715-16赛季CBA联赛之青岛
日期:2017-08-17 19:55:2415-16赛季CBA联赛之天津
日期:2017-06-29 10:34:4315-16赛季CBA联赛之四川
日期:2017-05-16 16:38:55黑曼巴
日期:2016-07-19 15:03:112015亚冠之萨济拖拉机
日期:2015-05-22 11:38:5315-16赛季CBA联赛之北京
日期:2019-08-13 17:30:53
34 [报告]
发表于 2018-02-03 10:55 |只看该作者
本帖最后由 523066680 于 2018-02-03 16:33 编辑

回复 33# 情节可以很简单

多谢打赏。CU 这方面还可以。就是人气越来越少了。

要带坐标的话,把字符间隔改为 tab 然后整个粘贴到 excel,这样不用考虑坐标字符宽度。

Tab:
  1. use List::Util qw/max min/;
  2. STDOUT->autoflush(1);

  3. my @data = read_file("bgr_trim8.txt");

  4. our ( @X, @Y, @Z, %edge );
  5. for my $row ( @data )
  6. {
  7.     $row=~/\((-?\d+),(-?\d+)\)\s+(\w+)/;
  8.     push @X, $1;
  9.     push @Y, $2;
  10.     push @Z, $3;
  11. }

  12. %edge = (
  13.     'x' => { 'min' => min( @X ), 'max' => max( @X ) },
  14.     'y' => { 'min' => min( @Y ), 'max' => max( @Y ) },
  15. );

  16. our @buffer;
  17. create_buffer();
  18. draw_buffer();
  19. #<STDIN>;

  20. sub draw_buffer
  21. {
  22.     our ( @X, @Y, @Z, %edge );
  23.     my ($x, $y, $str);

  24.     # 数据填入
  25.     for my $id ( 0 .. $#X )
  26.     {
  27.         $x = $X[$id] - $edge{x}{min};
  28.         $y = $Y[$id] - $edge{y}{min};
  29.         $buffer[ $y ][ $x+1 ] = $Z[$id];
  30.     }

  31.     $str = join "\n", map { join("\t", @$_ ) } ( @buffer );
  32.     write_file("tab.txt", $str );
  33. }

  34. sub create_buffer
  35. {
  36.     my $dtx = $edge{x}{max} - $edge{x}{min} + 1;
  37.     my $dty = $edge{y}{max} - $edge{y}{min} + 1;

  38.     my $realy;
  39.     for my $row ( 1 .. $dty )
  40.     {
  41.         $realy = $edge{y}{min} + $row - 1;
  42.         push @buffer, [ $realy, ("") x $dtx, $realy ];
  43.     }

  44.     #添加 x 坐标轴
  45.     push @buffer, [ "", map { $_ } ( int($edge{x}{min}) .. int($edge{x}{max}) ) ];
  46.     unshift @buffer, $buffer[$#buffer];
  47. }

  48. sub read_file
  49. {
  50.     open my $fh, "<", $_[0];
  51.     return (<$fh>);
  52. }

  53. sub write_file
  54. {
  55.     open my $fh, ">", $_[0];
  56.     print $fh $_[1];
  57.     close $fh;
  58. }
复制代码

Excel 版:
  1. use Win32::OLE qw (in with);
  2. use Win32::OLE::Const ('Microsoft Excel');
  3. use Win32::OLE::Variant;
  4. use List::Util qw/max min/;
  5. STDOUT->autoflush(1);
  6. use Cwd;
  7. my $cwd = getcwd();
  8. $cwd=~s/\//\\/g;

  9. our $ex;
  10. our $book;
  11. our $sheet;
  12. create_sheet();

  13. my @data = read_file("bgr_trim8.txt");

  14. our ( @X, @Y, @Z, %edge );
  15. for my $row ( @data )
  16. {
  17.     $row=~/\((-?\d+),(-?\d+)\)\s+(\w+)/;
  18.     push @X, $1;
  19.     push @Y, $2;
  20.     push @Z, $3;
  21. }

  22. %edge = (
  23.     'x' => { 'min' => min( @X ), 'max' => max( @X ) },
  24.     'y' => { 'min' => min( @Y ), 'max' => max( @Y ) },
  25. );

  26. our @buffer;
  27. create_buffer();
  28. draw_buffer();
  29. save_sheet( $cwd ."\\data.xlsx" );

  30. sub draw_buffer
  31. {
  32.     our ( @X, @Y, @Z, %edge );
  33.     my ($x, $y, $str);

  34.     # 数据填入
  35.     for my $id ( 0 .. $#X )
  36.     {
  37.         $x = $X[$id] - $edge{x}{min};
  38.         $y = $Y[$id] - $edge{y}{min};
  39.         $buffer[ $y ][ $x+1 ] = $Z[$id];
  40.         $sheet->cells( $y+2, $x+2 )->{Value} = $Z[$id];
  41.     }

  42. }

  43. sub create_buffer
  44. {
  45.     my $dtx = $edge{x}{max} - $edge{x}{min} + 1;
  46.     my $dty = $edge{y}{max} - $edge{y}{min} + 1;

  47.     my $realy;
  48.     for my $row ( 1 .. $dty )
  49.     {
  50.         $realy = $edge{y}{min} + $row - 1;
  51.         $sheet->cells( $row+1, 1 )->{Value} = $realy;
  52.         $sheet->cells( $row+1, $dtx+2 )->{Value} = $realy;
  53.     }

  54.     #添加 x 坐标轴
  55.     for my $col ( 1 .. $dtx )
  56.     {
  57.         $realx = $edge{x}{min} + $col + 1;
  58.         $sheet->cells( 1, $col+1 )->{Value} = $realx;
  59.         $sheet->cells( $dty + 2, $col+1 )->{Value} = $realx;
  60.         $sheet->columns( $col )->{ColumnWidth} = 2;
  61.     }

  62.     $sheet->columns( $dtx+1 )->{ColumnWidth} = 2;
  63.     $sheet->columns( $dtx+2 )->{ColumnWidth} = 2;
  64. }

  65. sub create_sheet
  66. {
  67.     our $ex;
  68.     our $book;
  69.     our $sheet;
  70.     # use existing instance if Excel is already running
  71.     eval { $ex = Win32::OLE->GetActiveObject('Excel.Application') };
  72.     die "Excel not installed" if $@;

  73.     unless ( defined $ex ) {
  74.         $ex = Win32::OLE->new('Excel.Application', sub { $_[0]->Quit; })
  75.                 or die "Oops, cannot start Excel";
  76.     }

  77.     #new workbook
  78.     $book = $ex->Workbooks->Add;
  79.    
  80.     # write to a particular cell
  81.     $sheet = $book->Worksheets(1);
  82. }

  83. sub save_sheet
  84. {
  85.     my $file = shift;
  86.     our $ex;
  87.     our $book;
  88.     our $sheet;
  89.     # save and exit
  90.     $ex->{DisplayAlerts} = 'False';
  91.     $book->SaveAs( $file );
  92.     undef $sheet;
  93.     undef $book;
  94.     undef $ex;
  95. }

  96. sub read_file
  97. {
  98.     open my $fh, "<", $_[0];
  99.     return (<$fh>);
  100. }

  101. sub write_file
  102. {
  103.     open my $fh, ">", $_[0];
  104.     print $fh $_[1];
  105.     close $fh;
  106. }
复制代码

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP