Home > Back-end >  How do I write the each word into one single cels?
How do I write the each word into one single cels?

Time:12-15

I am trying to write data from json file to CSV file using python. My code is like this:

CSVFile1 = open('Group_A_participant_1_1.csv', 'a')
writeCSV1 = csv.writer(CSVFile1)

for file in data['annotations'][3]['instances']:
    var = file['arguments'].get('argument1')
    writeCSV1.writerow(var)
CSVFile.close()

My output is: enter image description here

So my problem is that I can not see the whole word in one cell.

Thanks your helps inn advance!

I expect to get each word in one single cell.

CodePudding user response:

Change

writeCSV1.writerow(var)

to

writeCSV1.writerow([var])

so you're writing an one-item list with your var instead of having the CSV module interpret var, a string, as separate characters.

For instance:

import csv
import sys

writeCSV1 = csv.writer(sys.stdout)

data = {
    "annotations": [
        {},
        {},
        {},
        {
            "instances": [
                {"arguments": {"argument1": "foo"}},
                {"arguments": {"argument1": "bar"}},
            ]
        },
    ],
}

for file in data["annotations"][3]["instances"]:
    var = file["arguments"].get("argument1")
    writeCSV1.writerow([var])

prints out

foo
bar

whereas taking the brackets out from around [var] results in

f,o,o
b,a,r

as you described.

CodePudding user response:

  1. Click on the first cell of the column where you want the converted names to appear (B2).
  2. Type equal sign (=), followed by the text “Prof. “, followed by an ampersand (&).
  3. Select the cell containing the first name (A2).
  4. Press the Return Key.
  5. You will notice that the title “Prof.” is added before the first name in the list.
  • Related