0

Imagine that I have a list like this :

list1 = [12.34, 17.68, 19.22]

Now I want to write each numbers in one row of a CSV file. So after that I changed the type of numbers to string, I do this :

with open(output_file_name, 'w') as ofile:
        outfile = csv.writer(ofile)
        outfile.writerows(list1)

and the result will be something like this :

1,2,.,3,4
1,7,.,6,8
1,9,.,2,2

How can I delete these commas between the numbers?

Note :

  1. I want to show each number in a row
  2. I don't want anything for delimiter.
  3. I use Python 3.9.7
  4. I know that .csv file might not be suitable for this case, but this is a project that somebody wants from me, as an exam !
7
  • 1
    Do you want them in a column or a row? Do you use Python 3.4+? Commented Sep 26, 2021 at 13:18
  • Go to the docs and search for the word delimiter to see all the ways to set this. Commented Sep 26, 2021 at 13:20
  • 1
    By definition, a CSV file is comma separated, is there an advantage in your use case to be gained by removing the separator? Commented Sep 26, 2021 at 13:21
  • @S3DEV, yeah but you can change it Commented Sep 26, 2021 at 13:22
  • @PCM - Yes, one can certainly change the defined separator used by the writer, but that’s not my point. A ‘CSV’ file is (by definition) comma separated. Therefore the OP does not want a CSV file. Commented Sep 26, 2021 at 13:24

5 Answers 5

2

You are passing list1 as the list of rows, so the csv module does its best to figure out what you mean.

Probably what you actually mean is

    outfile.writerow(list1)

(notice the singular writerow) which writes all the values as a single row.

Or, if you want one number per row, try

    outfile.writerows([x] for x in list1)

In your attempt, list1 is converted to a list of strings, and then each string is split into a sequence to produce a row.

To reiterate, the argument to csv.writer.writerow should be a list of cell values, and the argument to writerows should be a list of such rows.

Sign up to request clarification or add additional context in comments.

Comments

1

writerows will treat each element of the array as an row and attempt to iterate over the entries in that row, that is why for a string it splits it into individual characters. if you want to write each number on a separate line then use:

with open(output_file_name, 'w') as ofile:
        outfile = csv.writer(ofile)
        outfile.writerows(([str(i)] for i in list1))

Comments

0

Use pathlib to easily write things

from pathlib import Path

# create file and write text
out_file = Path('example.csv')

# data
list_1 = [12.34 , 17.68 , 19.22]

out_file.write_text('column1, ')
with out_file.open(mode='a') as f:
    # write to file
    {f.write(f'\n{item}, ') for item in list_1}
    
    
# read contents
contents = out_file.read_text()
print(contents)

2 Comments

This produces a file which technically is a CSV file as long as none of the rows contain quotes or commas. If the code should extend to arbitrary data, this won't work (though using CSV if you only have one value per line is silly, anyway; just save as a plain old text file, which is exactly what this does).
It can be extended to deal with all of that. Example we can do preprocessing of item before adding it. I will argue that this way, we have more control over what is written.
0
import csv

list1 = [12.34, 17.68, 19.22]

with open('output_file_name', 'w') as ofile:
    outfile = csv.writer(ofile, delimiter=" ")
    outfile.writerow(list1)

This should works for You I have checked it out in editor :-D Best regards ;-)

1 Comment

This is not what OP wants though.
-1

One quick way to do this, would be to make use of the .join() method. This method can take items from an iterator (such as your list) and join them together separated by whatever character(s) you'd like. You can do it like this to have your items separated by only a space:

with open(output_file_name, 'w') as ofile:
    outfile = csv.writer(ofile)
    outfile.writerows(' '.join(list1))

If you don't want a space between the items, but instead want them all stuck together do this instead:

outfile.writerows(''.join(list1))

2 Comments

Do you know how CSV works?
He doesn't want a comma separated value list, quite the opposite....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.