-1

I'm trying to create a CSV file with some text in it using playgrounds. This is the code:

// The text to save
let csvText = "Hello,World,Goodbye!"

// The path
var document = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
var fileURL = document?.appendingPathComponent("result.csv")

// Writing text to path
do {
    try csvText.write(to: fileURL!, atomically: true, encoding: .utf8)
} catch {
    print("something went wrong..")
}

The code does work - it makes a .CSV file with the text inside. The problem is that the text isn't placed in columns. See image below:

result

The text should be placed under column A B and C.

I thought you define columns by using commas, that's why the words in my csvText constant are separated by commas. Why doesn't this work?

Hapeki
  • 2,153
  • 1
  • 20
  • 36
  • 1
    You open your file with... Excel? Numbers? OpenOffice? When you open a CSV file, the app usually ask what separator to use, sometimes by default it's `;` or `,`. – Larme Jan 30 '18 at 13:43
  • Hey, could you upload your image to another dataabase? I can;t see it. It's blocked. I know I can hekp you. @Hapeki –  Jan 30 '18 at 13:44
  • @Larme Microsoft Excel, I never opened Excel on this machine and it never asked me which separator to use, does this matter? – Hapeki Jan 30 '18 at 13:45
  • 1
    https://support.office.com/en-us/article/import-or-export-text-txt-or-csv-files-5250ac4c-663c-47ce-937b-339e391393ba The part "Change the separator in all .csv text files In Microsoft Windows, click the Start button, and then click Control Panel. Open the dialog box for changing Regional and Language settings. Type a new separator in the List separator box. Click OK twice. Note: After you change the list separator character for your computer, all programs use the new character as a list separator. You can change the character back to the default character by following the same procedure.". – Larme Jan 30 '18 at 13:47
  • @Hapeki look at my comment/answer. –  Jan 30 '18 at 13:47
  • I guess it depends of locale? I don't know. But I think that whatever be the separator you choose, there will be always another guy whom won't use this one by default. – Larme Jan 30 '18 at 13:48
  • In Excel you have to **import** the data, not just *open* the file. As far as I know only semicolon separated files are opened in columns properly. – vadian Jan 30 '18 at 13:50

3 Answers3

1

When you import csv (in oppenOfice at least), you have to said the text separator and columns separator.

Make sure you use in your reader the good separator.

You can try with semicolon ';' which is a usually columns separator.

  • 1
    I didn't know that the program used matters. I used Excel and indeed I had to use ; to separate the items, which is .. Isn't that weird since it's called "Comma separated values" and not "Semicolon separated values"? Anyways I'll mark this as accepted in 10 minutes, thanks! – Hapeki Jan 30 '18 at 13:47
1

The CSV is a raw text file. When it is opened, the text will not (usually) appear in columns. That is where the table comes in. When a table or sheet-like program opens a CSV, it usually splits at newlines then further at commas. This creates a 2D array. Then, it writes the 2D array to the table/sheet.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
1

As others have said, you need to make sure you configure your spreadsheet program to use the desired delimiter.

I just tried it.

In Excel for Mac, there is an Import command in the file menu. It walks you through a whole series of options, and allows you to pick the desired delimiter. On my copy the default delimiter was a tab, but I was able to switch to comma-delimited data quite easily.

Duncan C
  • 128,072
  • 22
  • 173
  • 272