简体   繁体   中英

Java code to change specific value on every line in csv format file

Basically I exported a csv file from Joomla Virtuemart. The task is to convert currency on every line to new currency. We had EEKs (Estonian kroon's) in use but from that year we went on EUR (Euro) which currency is 15.64664 times bigger than EEKs. So I have to divide old price on each line with 15.64664 to convert it to EUR and then output the files just as it was before but know the prices are changed.

Basically my way of thought was to take the prices, put these in a separate array, divide these by 15.64664 and then write new values back to csv format lines in the right place.

The file looks like this (here are three separate lines):

~product_sku~^~product_parent_sku~^~category_path~^~product_name~^~product_s_desc~^~product_desc~^~product_price~^~product_full_image~^~product_thumb_image~^~product_availability~^~product_available_date~^~product_currency~^~product_discount~^~product_discount_date_end~^~product_discount_date_start~^~product_packaging~^~product_publish~^~product_special~^~product_tax~
~000358~^~~^~IT SEADMED (tellimisel)/Computing/PC Components/PSU~^~Codegen power supply ATX PSU 350W V2.03 P4 CE PFC PSU (SILENT/ 1~^~~^~Codegen power supply ATX PSU 350W V2.03 P4 CE PFC PSU (SILENT/ 12cm fan)~^~43.33~^~http://s3.acme.lt/products/000358/photos/large/1.jpg~^~http://s3.acme.lt/products/000358/photos/small/1.png~^~~^~13/10/2010 00:00:00~^~EUR~^~~^~~^~~^~0~^~Y~^~~^~20.00~
~000390~^~~^~IT SEADMED (tellimisel)/Computing/Peripherals/Keyboards~^~Chicony multimedia keyboard KU-0420, LT/RUS, USB, 17 multimedia ~^~~^~Chicony multimedia keyboard KU-0420, LT/RUS, USB, 17 multimedia hotkeys, slim shape design, thin keycaps, silver/black~^~27.50~^~http://s3.acme.lt/products/000390/photos/large/1.jpg~^~http://s3.acme.lt/products/000390/photos/small/1.png~^~~^~~^~EUR~^~~^~~^~~^~0~^~Y~^~~^~20.00~

First line is field names with name delimiters (~) and field delimiters (^). As you can see the price is on the seventh place of field names. Is it possible to write a java code which takes the price value (starting from second line, without field and name delimiters) puts it in an array. Then every value in that array is divided by 15.64664 and afterwards the old price values are replaced with new divided and formatted (two decimal places) values?

The data can be read from .txt or .csv file if it matters.

I would be grateful if someone could point me in the right direction. :)

You can try OpenCSV library. They also have sample code on their site.

You can import the file in Microsoft Excel or any other Spread Sheet program and then use the formula to make the changes in a new column or the same column and then export it as a CSV file.

That should be pretty quick.

I'd read the file line by line,then use a regular expression to get to the price, do the calculation and write the line back to another file with the new price.

Must it be java? Although this can easily be done in Java, perl/ruby and python are way better at these things (less lines of code at least).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM