This question already has answers here:
Efficient read and write CSV in Go
(3 answers)
Closed 3 years ago.
I have a CSV file that uses a space as the delimiter. But some of the fields contain a space and those fields are wrapped with double quotes. Any field with a null/empty value is represented as "-". Fields that are not null/empty and do not contain spaces are not wrapped in double quotes. Here's an example of one row in the CSV file.
foobar "foo bar" "-" "-" "-" fizzbuzz "fizz buzz" fizz buzz
Also there are no headers for the CSV file. I was going to use a simple solution such as this one https://stackoverflow.com/a/20769342/3299397 but using strings.Split(csvInput, " ") wouldn't handle the spaces inside the fields. I've also looked into this library https://github.com/gocarina/gocsv but I'm curious if there's a solution that doesn't use a third-party library.
This is "plain" CSV format where the separator is the space character instead of comma or semicolon. The encoding/csv package can handle this.
As to your null / empty fields: just use a loop as a post-processing step and replace them with the empty string.
Using the input:
const input = `foobar "foo bar" "-" "-" "-" fizzbuzz "fizz buzz" fizz buzz
f2 "fo ba" "-" "-" "-" fd "f b" f b`
Parsing and post-processing it:
r := csv.NewReader(strings.NewReader(input))
r.Comma = ' '
records, err := r.ReadAll()
if err != nil {
panic(err)
}
fmt.Printf("%#v\n", records)
for _, r := range records {
for i, v := range r {
if v == "-" {
r[i] = ""
}
}
}
fmt.Printf("%#v\n", records)
Output (try it on the Go Playground):
[][]string{[]string{"foobar", "foo bar", "-", "-", "-", "fizzbuzz", "fizz buzz", "fizz", "buzz"}, []string{"f2", "fo ba", "-", "-", "-", "fd", "f b", "f", "b"}}
[][]string{[]string{"foobar", "foo bar", "", "", "", "fizzbuzz", "fizz buzz", "fizz", "buzz"}, []string{"f2", "fo ba", "", "", "", "fd", "f b", "f", "b"}}
Related
I want a cell in Microsoft Excel (of type: dropdown list of strings) to fetch data from -
an API endpoint returning JSON response of array of strings (this format can be changed)
eg. response:
[
"Oranges",
"Apples",
"Mangoes"
]
I want something like : Set the formula of the cell to FetchList("localhost:8080/api/v1/list").
(FetchList is randomly written.)
How can I get started to achieve this ?
Thanks for any help!
One way to do this with Office 365 is place a formula somewhere in your workbook that contains the values that you want to present in the dropdown and then refer to that formula.
The formula would be:
= TRANSPOSE(
TEXTSPLIT(
SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( J2, """", "" ), "[", "" ), "]", "" ),
"," ) )
Where J2 contains the JSON string ["Oranges","Apples","Mangoes"] that you fetched. (i.e. the result of "localhost:8080/api/v1/list")
If you put that in cell D2 for example, then you can go to Data > Data Validation and choose List with the formula being =$D$2#.
A more readable version of the formula might be:
=LET( s, J2,
s_cln, SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( s, """", "" ), "[", "" ), "]", "" ),
TRANSPOSE( TEXTSPLIT( s_cln, "," ) ) )
Why is data serialized using JSONSerialization differ from the data serialized with the extension below?
let uint8Array: [UInt8] = [123, 234, 255]
let data1 = uint8Array.data // 3bytes
let data2 = try! JSONSerialization.data(withJSONObject: uint8Array) // 13 bytes
extension Data {
var bytes: [UInt8] {
return [UInt8](self)
}
}
extension Array where Element == UInt8 {
var data: Data {
return Data(self)
}
}
JSON, and so the JSONSerialization will transform an object (here an array of Int), into Data according to some rules (like adding "[", "]", "," in our cases, in others input values and options, it can add if necessary ":", "{", "}", "\n"`, etc) and using the UTF8 (or kind of UTF16, but I'll skip this part for understanding, see this part in WikiPedia) encoding.
So in fact, for 123 value, it will be 1 into UTF8 encoding, then 2, then 3, so just for one number, it's 3 bytes.
So you see that for your 3 Int numbers, it will be 9 bytes, then, we need to add the "[", "]", and the two commas to separate them: ",", which makes 9 + 2 + 2 = 13 bytes.
To illustrates:
Let's add:
extension Data {
func hexRepresentation(separator: String) -> String {
map { String(format: "%02hhX", $0) }.joined(separator: separator)
}
func intRepresentation(separator: String) -> String {
map { String(format: "%d", $0) }.joined(separator: separator)
}
}
And use it to see what are the values inside data1 & data2:
print(data1.hexRepresentation(separator: "-"))
print(data1.intRepresentation(separator: "-"))
print(data2.hexRepresentation(separator: "-"))
print(data2.intRepresentation(separator: "-"))
$>7B-EA-FF
$>123-234-255
$>5B-31-32-33-2C-32-33-34-2C-32-35-35-5D
$>91-49-50-51-44-50-51-52-44-50-53-53-93
I let you chose as you prefers Int or hex raw value interpretations.
We see that using the first method, we get 3 bytes, with the initial values of your array.
But for the JSON one, we can split it as such:
$>91 -> [
$>49-50-51 -> 1, 2 & 3
$>44 -> ,
$>50-51-52 -> 2, 3, 4
$>44 -> ,
$>50-53-53 -> 2, 5, 5
$>93 -> ]
You can check on any UTF8 table (like https://www.utf8-chartable.de, etc), but 91 that for [, 49 it's for 1, etc.
I have a question in regards to formatting a file so that it displays a Json output to the correct format.
At the moment the code I have below imports a json into a file but when I open the file, it displays the json in a single line (word wrap unticked) like so:
{"products":[{"type":null,"information":{"description":"Hotel Parque La Paz (One Bedroom apartment) (Half Board) [23/05/2017 00:00:00] 7 nights","items":{"provider Company":"Juniper","provider Hotel ID":"245","provider Hotel Room ID":"200"}},"costGroups":[{"name":null,"costLines":[{"name":"Hotel Cost","search":null,"quote":234.43,"quotePerAdult":null,"quotePerChild":null}
I want to format the json in the file so that it looks like actual json formatting like so:
{
"products": [
{
"type": null,
"information": {
"description": "Hotel Parque La Paz (One Bedroom apartment) (Half Board) [23/05/2017 00:00:00] 7 nights",
"items": {
"provider Company": "Juniper",
"provider Hotel ID": "245",
"provider Hotel Room ID": "200"
}
},
"costGroups": [
{
"name": null,
"costLines": [
{
"name": "Hotel Cost",
"search": null,
"quote": 234.43,
"quotePerAdult": null,
"quotePerChild": null
}
Virtually each header has its own line to contain its values.
What is the best way to implement this to get the correct json formatting within the file?
Below is the code:
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def dataFolder = groovyUtils.projectPath +"//Log Data//"
def response = testRunner.testCase.getTestStepByName("GET_Pricing{id}").getProperty("Response").getValue();
def jsonFormat = (response).toString()
def fileName = "Logged At - D" +date+ " T" +time+ ".txt"
def logFile = new File(dataFolder + fileName)
// checks if a current log file exists if not then prints to logfile
if(logFile.exists())
{
log.info("Error a file named " + fileName + "already exisits")
}
else
{
logFile.write "Date Stamp: " +date+ " " + time + "\n" + jsonFormat //response
If you have a modern version of groovy, you can do:
JsonOutput.prettyPrint(jsonFormat)
I am struggling with that issue for a quite long time.
In fact, I have a QR Code in which I have this text:
{ "Version ": 0x1, "Type ": "MapPoint ", "X ": 2, "Y ": 3}
Then I transform the content of this QR code to a Json Object:
JSONObject scanQRCode = new JSONObject(contents);
When I debug, I find that the string contents is like that:
" { \"Version \": 0x1, \"Type \": \"MapPoint \", \"X \": 2, \"Y \": 3} "
And ScanQRCode equals to that:
{{"Version ":1,"Type ":"MapPoint ","X ":2,"Y ":3}} Org.Json.JSONObject
And the in my code I have to verify this condition and get the double X and Y as following:
if (scanQRCode.Has("Version") && scanQRCode.GetInt("Version") >= 0x1 && scanQRCode.Has(KEY_QR_TYPE) && scanQRCode.GetString("Type").Equals(("MapPoint")))
{
float x = (float)scanQRCode.GetDouble("X");
float y = (float)scanQRCode.GetDouble("Y");
}
the problem is that condition is never fulfilled and I can't even get for example the double X.
Can you please tell me what is wrong?
{ "Version": "0x1", "Type":"MapPoint", "X": 2, "Y": 3}
This is already a json object no need to convert it again you can directly use it as follow
var a = { "Version": "0x1", "Type":"MapPoint", "X": 2, "Y": 3}
float x = a.X;
How convert JSON to CoffeeScript and write on a file ".coffee" with NodeJS?
JSON:
{
"name": "jack",
"type": 1
}
to CoffeeScript:
"name": "jack"
"type": 1
Should be easy enough by traversing the object (for … of). Just use recursion and take the indent level as an argument:
esc_string = (s) ->
return '"' + s.replace(/[\\"]/g, '\\$1') + '"'
csonify = (obj, indent) ->
indent = if indent then indent + 1 else 1
prefix = Array(indent).join "\t"
return prefix + esc_string obj if typeof obj is 'string'
return prefix + obj if typeof obj isnt 'object'
return prefix + '[\n' + (csonify(value, indent) for value in obj).join('\n') + '\n' + prefix + ']' if Array.isArray obj
return (prefix + esc_string(key) + ':\n' + csonify(value, indent) for key, value of obj).join '\n'
Test case:
alert csonify
brother:
name: "Max"
age: 11
toys: [
"Lego"
"PSP"
]
sister:
name: "Ida"
age: 9
Result:
"brother":
"name":
"Max"
"age":
11
"toys":
[
"Lego"
"PSP"
]
"sister":
"name":
"Ida"
"age":
9
No live demo, since I don't know a JSFiddle for CoffeScript.
Live demo: http://jsfiddle.net/vtX3p/
I hope you know how to read and write files in nodejs, so i will not address that here.
To convert javascript to coffeescript you can use this npm:
https://github.com/rstacruz/js2coffee