I am attempting to write a multi-dimensional array to an Excel file (extension .xls) using the WriteExcel Ruby gem on El Capitan.
My attempt:
# -*- coding:utf-8 -*-
require 'writeexcel'
# Create a new Excel workbook
workbook = WriteExcel.new('ruby.xls')
# Add a worksheet
worksheet = workbook.add_worksheet
eec = [
['maggie', 'milly', 'molly', 'may' ],
[13, 14, 15, 16 ],
['shell', 'star', 'crab', 'stone']
]
worksheet.write_col('A1', \#eec)
workbook.close
The error I get:
iMac:scrapScripts guy$ ruby script.rb
script.rb:16: syntax error, unexpected $undefined, expecting ')'
worksheet.write_col('A1', \#eec)
^
Using the gem documentation found here.
Can anyone explain why this syntax error is occurring, and how I can fix it?
Remove garbage symbols from the call to write_col:
- worksheet.write_col('A1', \#eec)
+ worksheet.write_col('A1', eec)
Related
I am using Sphinx 4.4.0 with napoleon extension (Google Doc String). I have this two problems
ARNING: Block quote ends without a blank line; unexpected unindent.
ERROR: Unexpected indentation.
I found something about it on the internet but can not fit this two my code. My problem is I even do not understand the messages. I do not see where the problem could be.
This is the code:
def read_and_validate_csv(basename, specs_and_rules):
"""Read a CSV file with respect to specifications about format and
rules about valid values.
Hints: Do not use objects of type type (e.g. str instead of "str") when
specificing the column type.
specs_and_rules = {
'TEMPLATES': {
'T1l': ('Int16', [-9, ' '])
},
'ColumnA': 'str',
'ColumnB': ('str', 'no answer'),
'ColumnC': None,
'ColumnD': (
'Int16',
-9, {
'len': [1, 2, (4-8)],
'val': [0, 1, (3-9)]
}
}
Returns:
(pandas.DataFrame): Result.
"""
This are the original messages:
.../bandas.py:docstring of buhtzology.bandas.read_and_validate_csv:11: WARNING: Block quote ends without a blank line; unexpected unindent.
.../bandas.py:docstring of buhtzology.bandas.read_and_validate_csv:15: ERROR: Unexpected indentation.
.../bandas.py:docstring of buhtzology.bandas.read_and_validate_csv:17: ERROR: Unexpected indentation.
.../bandas.py:docstring of buhtzology.bandas.read_and_validate_csv:19: WARNING: Block quote ends without a blank line; unexpected unindent.
.../bandas.py:docstring of buhtzology.bandas.read_and_validate_csv:20: WARNING: Block quote ends without a blank line; unexpected unindent.
reStructuredText is not Markdown, and indentation alone is not enough to demarcate the code block. reStructuredText calls this a literal block. Although the use of :: is one option, you might want to explicitly specify the language (overriding the default) with the use of the code-block directive.
Also I noticed that you have invalid syntax in your code block—a missing ) and extra spaces in your indentation—which could have caused those errors.
Try this.
def read_and_validate_csv(basename, specs_and_rules):
"""Read a CSV file with respect to specifications about format and
rules about valid values.
Hints: Do not use objects of type type (e.g. str instead of "str") when
specificing the column type.
.. code-block:: python
specs_and_rules = {
'TEMPLATES': {
'T1l': ('Int16', [-9, ' '])
},
'ColumnA': 'str',
'ColumnB': ('str', 'no answer'),
'ColumnC': None,
'ColumnD': (
'Int16',
-9, {
'len': [1, 2, (4-8)],
'val': [0, 1, (3-9)]
}
)
}
Returns:
(pandas.DataFrame): Result.
"""
I'm having these json parsing errors from time to time:
2022-01-07T12:15:19,872][WARN ][logstash.filters.json ] Error parsing json
{:source=>"message", :raw=>" { the invalid json }", :exception=>#<LogStash::Json::ParserError: Unrecognized character escape 'x' (code 120)
Is there a way to get the :exception field in the logstash config file?
I opened the exact same thread on the elastic forum and got a working solution there. Thanks to #Badger on the forum, I ended up using the following raw ruby filter:
ruby {
code => '
#source = "message"
source = event.get(#source)
return unless source
begin
parsed = LogStash::Json.load(source)
rescue => e
event.set("jsonException", e.to_s)
return
end
#target = "jsonData"
if #target
event.set(#target, parsed)
end
'
}
which extracts the info I needed:
"jsonException" => "Unexpected character (',' (code 44)): was expecting a colon to separate field name and value\n at [Source: (byte[])\"{ \"baz\", \"oh!\" }\r\"; line: 1, column: 9]",
Or as the author of the solution suggested, get rid of the #target part and use the normal json filter for the rest of the data.
I'm trying to parse a Unity YAML scene file with Ruby and the Psych gem. The Unity file is supposedly a subset of YAML. Each Unity object is a separate YAML document. Here's a sample:
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!29 &1
OcclusionCullingSettings:
m_ObjectHideFlags: 0
serializedVersion: 2
m_OcclusionBakeSettings:
smallestOccluder: 5
smallestHole: 0.25
backfaceThreshold: 100
m_SceneGUID: 00000000000000000000000000000000
m_OcclusionCullingData: {fileID: 0}
--- !u!104 &2
RenderSettings:
m_ObjectHideFlags: 0
serializedVersion: 9
m_Fog: 0
m_FogColor: {r: 0.5, g: 0.5, b: 0.5, a: 1}
m_FogMode: 3
m_FogDensity: 0.01
I'm parsing it with:
require 'psych'
scene = Psych.load_stream(File.read "./Scene.unity")
Which results in this error message:
Psych::SyntaxError ((<unknown>): found undefined tag handle while parsing a node at line 13 column 5)
After some investigation it appears that Psych is expecting each YAML document to have it's own TAG directive, while Unity's YAML parser must consider the directives at the beginning of the file to be global. So...
Question 1: Who's right? Does the scope of a TAG directive extend from declaration to the end of the file, or only to the end of the document (the next occurrance of ---)?
I'd like to be able to parse the file without having to modify it. This leads to...
Ouestion 2: Is there a clean way to do this? Say a Psych option I've missed or a way to pass a block to File.read so that I can add TAG directives on the fly?
I wanted to use multi select list in the excel. hope the excel should contain vb macros for that. So I am trying to add the macros to the excel dynamically via ruby. But I didt get any idea. Is there any other option to generate the excel with single and multi select list option in ruby on rails.
I am using ubuntu and the libreoffice. I have manually tried to add the macro vb script in the excel in windows and it was working. But I cant do it with ruby. Please suggest if any other solutions are there
require 'writeexcel'
#data_array = ["option","aaa","bbb","ccc","ddd","eee","fff","ggg"]
workbook = WriteExcel.new('single_select_dropdown.xls')
worksheet = workbook.add_worksheet('sheet1')
worksheet.set_column('A:A', 30)
worksheet.set_column('B:B', 30)
worksheet.set_column('C:C', 20)
worksheet.set_column('A:D', 30)
worksheet.write(0, 0, 'Select Your Option')
worksheet.write(0, 1, 'option')
worksheet.data_validation(0, 1,
{
:validate => 'list',
:source => #data_array
})
worksheet.write(10, 2, "Your option is")
worksheet.write(10, 3, "=B1")
workbook.close
and this is for single select dynamic list via ruby, and still am trying multi select list
require "rubygems"
require 'write_xlsx'
#data_array = ["aaa","bbb","ccc","ddd","eee","fff","ggg"]
workbook = WriteXLSX.new('multiple_select_vba_project.xlsm')
workbook.set_vba_name('Workbook')
worksheet = workbook.add_worksheet
worksheet.set_vba_name('Sheet1')
worksheet.activate() #don't forgot to add this
worksheet.set_column('A:A', 30)
worksheet.set_column('C:C', 30)
workbook.add_vba_project('vbaProject.bin')
worksheet.write(1, 0, 'select your option')
worksheet.write(1, 2, 'option')
worksheet.data_validation(1, 2,
{
:validate => 'list',
:source => #data_array
})
workbook.close
also you need vbaProject.bin file..
need to extract vbaProject.bin file from existing xls file use this command
extract_vba filename.xlsm
if suppose got any error please update your libexcel-writer-xlsx-perl gem using this command
sudo apt-get -f install
I have loaded a string from a html.file, and I have writen it to a yaml file with the plugin ya2yaml:
- title: 'What a wonderful day!'
body: ... # main contents here
and I will load the .yml file by YAML::parse_file method.
but "\n" in the string will cause load problems, so I tried to gsub all "\n" to "", but there is still problems, a char '0083'(I see it in the terminal) still breaks the line, and cause loading problem:
in `load': syntax error on line 32, col 6: ` </strong><br>ok ' (ArgumentError)
from /home/croplio/.rvm/rubies/ruby-1.9.2-preview3/lib/ruby/1.9.1/syck.rb:178:in `parse'
from /home/croplio/.rvm/rubies/ruby-1.9.2-preview3/lib/ruby/1.9.1/syck.rb:203:in `block in parse_file'
from /home/croplio/.rvm/rubies/ruby-1.9.2-preview3/lib/ruby/1.9.1/syck.rb:202:in `open'
So what's wrong with the yaml or the char 0083?
or how can I avoid this problem?
0083 is a unicode character 'NO BREAK HERE'.
I don't know YAML::pars, but maybe you can switch it to use unicodes or use pure ascii codes.