I am reading in a CSV file with Ruby and it works fine except if there are blank rows at the start. It seems to skip the blank lines and go straight to a line with content. I want to read the blank lines as I need to count the row numbers.
I use this function, which works fine except for skipping the blank rows:
CSV.foreach(params[:file].tempfile, headers: false, :encoding => 'ISO-8859-1') do |row|
I have a user input which dictates the starting row to read in, so I need to count down each row until I reach the desired row to read. I really do not want to tell the user to edit the csv file to make sure rows are not blank.
UPDATE:
OK, my question turned out to be a red herring. The actual problem was that I had an Excel file with blank lines at the start. When I saved as a CSV file from within Excel, the blank lines were being removed from the saved CSV file but the Excel view of the file, i.e. filename.csv, still showed the blank lines present. I only discovered this by re-opening the file in Excel or a text editor. (All posted comments helped give me the clues to find this).
So ... now I have a new problem ... why does Excel remove blank rows when saving as CSV?
OK, my question turned out to be a red herring. The actual problem was that I had an Excel file with blank lines at the start. When I saved as a CSV file from within Excel, the blank lines were being removed from the saved CSV file but the Excel view of the file, i.e. filename.csv, still showed the blank lines present. I only discovered this by re-opening the file in Excel or a text editor.
Related
When I try to save edited node Drupal just reset field values and do nothing without any error message or log.
I entered text word by word and found that word "having" cause such behavior.
I'm using ckeditor to edit and filter text value and I guess that this module source of problem. As if I save text as plain text there are no issues.
Right now I don't know what to do next to track, dig deeper and isolate this issue...
PS. In ckeditor format settings I checked only two options:
Limit allowed HTML tags and correct faulty HTML
Convert line breaks into HTML (i.e. br and p)
I have a text file named data.txt having various parameters such as number, status and so on.
Each line contains a different set of data.
Now, in my GUI, I have submit button. On click of that button, I want 'few' of these data to be displayed as a table on the GUI screen
One thing is I do know which position it exists in the text file :
Ex: Status appears at position [70..78] in each line of text file
Number appears at position [85..90] in each line of text file and so on
I want only those particular parts of data to be displayed in respective columns of the table
Any suggestions would be welcome. I am using Qt and Ruby for my GUI design
Following images show what exactly i am looking for:
EDIT: Using the solution mentioned by Stephen:
Using the line position worked for me. But one query here, when i try to use that using puts statement, it does print properly. However when i try to use the same for displaying it inside TextEdit widget, each line does not get displayed..
#text_var = "#{line[70..78]}\t#{line[85..90]}
#text = Qt::TextEdit.new(self)
#text.setText (#text_var)
puts #text_var
Puts statement gives correct output, however i am not able to send the same inside the widget.
If i use break statement , then first line is getting displayed in widget correctly. So error is happening when it is trying to read line by line on to the widget. Ultimately, it is getting overwritten and last line where just blank spaces are there is getting printed in the widget i feel.
Any solutions for this ?
It looks like the Status and Number you want always appear in the same column. You can split each line of data.txt on whitespace to only display the column you want.
File.open('data.txt').readlines.each do |line|
columns = line.split(/\s+/)
puts "#{columns[4]}\t#{columns[5]}"
end
This prints the 5th and 6th columns, separated by a tab.
You could also take advantage of knowing the position, as you mention:
File.open('data.txt').readlines.each do |line|
puts "#{line[70..78]}\t#{line[85..90]}"
end
I am trying to open an existing .xls file and overwrite the contents in one spreadsheet (tab).There are many tabs on the file and many have pivottables and other visual presentations.
I have tried Spreadsheet and axlsx. Axlsx has great controls but overwrites the entire file including any other created tabs. Spreadsheet will open and edit a file but you have to copy the other tabs which will remove the excel formatting.
Is there a way to use Ruby to add data to only one tab in a spreadsheet without changing content in the other tabs?
Update:
Here is what I am testing now using the Spreadsheet gem. I can open a spreadsheet that has multiple tabs where one tab contains a pivottable, another contains a chart, and another the raw data. They have to be saved out as a new doc otherwise you get a File Format is not Valid error.
open_book = Spreadsheet.open('../data/exports/test_output_dashboard.xls')
puts "#{open_book.worksheet(0)}"
puts "#{open_book.worksheet(1)}"
puts "#{open_book.worksheet(2)}"
open_book.write('../data/exports/test_output_dashboard_2.xls')
If I just open and resave the new document is fine, a working copy of the original. However, if I edit the tab with the raw data as in this code then when I open the file it is shown as needs to be 'repaired' and none of the tabs show the correct information.
open_book = Spreadsheet.open('../data/exports/test_output_dashboard.xls')
puts "#{open_book.worksheet(0)}"
puts "#{open_book.worksheet(1)}"
puts "#{open_book.worksheet(2)}"
new_row_index = open_book.worksheet(1).last_row_index + 1
open_book.worksheet(1).insert_row(new_row_index, row_2)
open_book.write('../data/exports/test_output_dashboard_4.xls')
Any suggestions for adding data to one tab of an excel doc while keeping the other tabs intact would be greatly appreciated. The solution can be any gem or could be any language or automatable tool.
UPDATE:
Here is an example Excel dashboard that I am using for testing. I am writing rows into the data tab. https://dl.dropboxusercontent.com/u/23226147/test_output_dashboard.xlsx
UPDATE:
With RubyXL I can open and inspect the content of each tab but the saved doc cannot be opened by Excel.
workbook = RubyXL::Parser.parse("../data/exports/test_output_dashboard.xlsx")
puts "#{workbook.worksheets[0].inspect}"
puts "#{workbook.worksheets[1].inspect}"
puts "#{workbook.worksheets[2].inspect}"
workbook.write("../data/exports/test_output_dashboard_5.xlsx")
If you're just looking for a quick tool, RubyXL might do the trick for you:
https://github.com/weshatheleopard/rubyXL
It parses existing .xlsx .xlsm files and has a decent set of documentation.
You could try cloudxls.com API. You can merge data into existing xls and xlsx files using its API. If that is not an option, you most likely have to use some java libraries like Apache POI.
Solution for Windows users only.
I use to modify Excel spreadhseet with the gem win32ole and it works fine.
In case it is interesting for you here is a short sample to open a file and activate a given tab:
excel = WIN32OLE.new('Excel.Application')
excel.visible = true
filepath = 'e:\tmp\file.xlsx'
cur_book = excel.workbooks.Open(filepath)
sheet_name = 'sheet1'
cur_sheet = cur_book.Worksheets(sheet_name)
# put value 10 in Cell(2,2)
cur_sheet.Cells(2,2).Value = 10
Official documentation: http://ruby-doc.org/stdlib-1.9.3/libdoc/win32ole/rdoc/WIN32OLE.html
Troubleshooting Magmi by way of the least-common-denominator, I exported a framework CSV from one Magento site with Dataflow, then immediately re-imported it with Magmi on another Magento site. I got an error:
CSV Datasource v1.2 - warning: line 1 , wrong column number : 25 found over 1, line skipped
I'm guessing there is something in the site's Magmi setup that I can't quite see, that is controlling any other imports? This second site already uses Magmi to import products on a nightly basis. I see the error above using both its established profile, and any other profile I create.
Magmi, by default, is set to process CSV files with each column enclosed in a double quote ".
If you look under the Configure Current Profile section, you'll find a filed labeled CSV Enclosure, with a double quote populated.
On the other hand, Magento exports the CSV files without the double quote wrappers. Try removing the double quote from the CSV Enclosure field in Magmi, then saving the profile. Do a test run and see if that works.
If this doesn't work, you can import the file into Excel and resave the file using double quotes. The method that works for me goes as follows:
Open Excel (create a blank spreadsheet)
Go to the Data tab in Excel, click the From Text button.
Open your CSV exported from Magento
Choose Delimited and click Next
Under Delimiters, select Comma. Click Next
On the Data Preview section, highlight all of the columns (Hold shift and select the farthest column on the right).
Choose Text under Column data format
Click Finish
After you verified all of your data is correct in the spreadsheet, you'll have to export it to wrap all of the cell values in double-quotes. To do this, follow this tutorial:
http://www.markinns.com/articles/full/export_excel_csvs_with_double_quotes
After the file is saved, upload it to your var/import directory, set Magmi to use " as the CSV Enclosure, and try running the import.
I have a multi-page ascii file, and I need to be able to separate this file into single page ascii files.
At the end of each page should be a page break inserted. My original thoughts are to read up to a page break, output to file, but then how would I make it read to a page break and then continue to start over reading from that page break to the next? Or is there another, easier way of doing this?
csplit allows you to split a file by regex.