CSV coming out as pipe-delimited - ruby-on-rails

I created a Rails file outputting values separated by commas, called posts.csv.erb.
When I click on the link, it downloads as a CSV file, but the commas are now pipes. Also, Vim tries to auto-correct the commas into pipes for some reason (but I got around that, so they are actually commas). It works fine if I call the file posts.txt.erb - I see actual commas. But somehow it seems Rails or ERB wants to output pipes for a CSV file?

Related

CSV read breaking when last line is \n for some reason

I'm working on a project with some supplied CSV files that I need to parse and do some manipulation on. One is throwing this error when I try to load it into a file using CSV.read('path/file.csv')
CSV::MalformedCSVError: Unquoted fields do not allow \r or \n (line 7911).
Now when looking at the file, the last line is just blank. It's a \n character. I feel like this should not break the CSV read but it is. Now, I could just check the end of the CSV documents and strip any access return carriages/new lines since that seems like it'll work but it doesn't seem like the correct way. Anybody have some advice?
Edit: Using Ruby 2.0.0 and Rails 4.0.5

Maintaining GREP variables through find and replace

I've recently taken on a project of document conversion to HTML. That is, a client gives me a .DOC file, and I need to convert the contents to one long HTML file - no styling, no CSS, just clean HTML with paragraph tags, header tags tags, etc.
I found an application that does a pretty good job of automating the first part of it. The problem is that I need to do some advanced find and replace based on strings using variables.
For instance, I have footnotes that were converted properly. They're currently displayed as superscript numbers with the
I'd like to change how the footnote is displayed. Instead of a superscript number 6 for the 6th footnote, I'd like it to show (Note 6)
To do that on the entire document (hundreds of footnotes), I'm wondering if I can do something like:
FIND:
<sup><a name="FN[0-9]" href="FNR[0-9]">[0-9]</a></sup>
REPLACE:
<a name="FN%1" href="FNR%2">(Note %3)</a>
The problem is, I can't find a Find and Replace tool that lets me maintain the variables in the replace area. All I get is the superscript 6 appearing as (Note %3), as well as every other footnote doing the same thing.
Anyone have any ideas on how I can accomplish my task efficiently?
In Perl it would look roughly like this on the command line (I have NOT tested this):
perl -i -p -e's{<sup><a name="(FN\d)" href="(FNR\d)">(\d)</a></sup>}{<a name="$1" href="$2">(Note $3)</a>}' filenames....
-i says "Edit this file in place", -p means "print each line after we do whatever is in the -e switch".
That's assuming you're only looking for a single digit where you have [0-9]. If you want to match FN427, then you change (FN\d) to (FN\d+), for example.
This also assumes that the HTML that are you parsing looks EXACTLY LIKE THAT. If you get some HTML that is <a href=... name=... (with the attributes in opposite order than you have) then it will break. In that case, you'll want to use an HTML parser.
I hope that gives you enough to start with.

How would you implement a db table of a list of all US zip codes in a rails application?

How would migrations be involved? Would you load them into the mysql db directly or have a ruby routine doing this.
To get the raw data I'd simply search on Google, you'll find lots of databases of zip codes, some of them are not free though.
Then I'd look at the data to get a clue on what columns I should include in the table, and build an appropriate migration and model.
Then I'd write an external ruby script which reads the data from whatever format the zip code database is in and writes it directly into the app's database. You could also do that as part of your Rails application, but I usually don't consider it necessary when dealing with external data only.
It's important, however, that the zip code table is not referenced by ID in some other table, since that makes it really complicated if you want to update it later (zip codes change). So I'd still store the zip code itself in, say, the user table, or wherever.
Here is a CSV link to all the zipcodes in the US: here . This file has 7 columns for each zipcode, with the zipcode being in the first column.
Now you could use the ruby CSV parser, or something like FasterCSV to parse the CSV, but I think it would be much faster if you simply parsed the CSV using a shell command. For example, I just ran this on my system and it instantly parses the file correctly:
cut -d ',' -f 1 zip_codes.csv > out.csv
At this point, it's a simple matter of reading in the file line by line in ruby like so:
File.open( out.csv ).each do |line|
Zipcode.create(:zip => line)
end
You will replace Zipcode.create.. with whatever model you are using, since you probably do not need a seperate Zipcode model.

Rails: Hash#to_json - split over multiple lines?

I'm having a slight annoyance with the javascript_I18n plugin, which generates js-friendly versions of the I18n translation tables, so that you can localise your javascript. It all works fine, but it works by calling to_json on each locale's translations hash and outputting the results into a file. When you call to_json on a hash the resulting string is all a single lne, which means you end up with files with an enormous line at the end.
This in turn is stopping git from being able to merge any changes, because the merge works on a line by line basis and can't deal with a single massive line with changes in the middle somewhere. It's not a massive problem because i can always just re-generate the js-friendly translation file with a rake task that comes with the plugin (which replaces the mid-merge file with a brand new one which i can just commit in), but it's just a bit annoying. It occurred to me that if the json was output on different lines, instead of all the same line, then it wouldn't be a problem, and it wouldn't even make the file that much bigger, just inserting two characters (\n) per line.
Before i try and hack the resulting string with a gsub to split it onto seperate lines, is there a nicer way to call to_json on a hash and output the results onto seperate lines? Or a nicer way to solve this problem generally? (i can't find much useful in the documentation for javascript_I18n).
Grateful for any advice - max
Not to answer you question, but to give a suggestion:
It would probably be easier to just ignore all the generated js translation files.
This is because even if you separate the translation into multiple lines, there are still chances of merge conflicts, which you probably already resolved once in your yaml file.
I would set it up this way:
1). gitignore all the js translation files.
2). In ActionController, add a before filter to generate the js files automatically on each load in development mode only.
3). Tweak your deploy.rb file to generate the js files after code update.
No more merge conflicts! (at least for js translation files) :D
Aaron Qian

How to reformat the mp3 filename which is saved through paperclip

I upload/save mp3 files through Paperclip, it transforms the name with underscores when it saves it.
For example if I upload "Gould Stokowski 1.mp3" it saves into the the db as "Gould_Stokowski_1.mp3". How can I take out the underscores (replace them with spaces" when I retrieve the file and I want to display the name.
What does the program do with the characters that started out as underscores? If it does nothing, then there is no way to go back using just the file name. The names don't "round trip."
If you're not concerned with that, then your question really has nothing to do with Paperclip or MP3 files at all. You just need to know how to change all the underscores into spaces. You can use String#tr for that:
$ irb
>> "Gould_Stokowski_1.mp3".tr('_', ' ')
=> "Gould Stokowski 1.mp3"

Resources