Question

I wish to write a simple text file with 6 lines in Python 2.7. I'm using this code:

import csv
export=open('./images2/test.tfw', "wb")
writer=csv.writer(export, delimiter=' ', quoting=csv.QUOTE_NONE)
writer.writerow('0.06')
writer.writerow('0.00')
writer.writerow('0.00')
writer.writerow('-0.06')
writer.writerow('-10.59')
writer.writerow('38.49')
export.close()

And I'm getting inside the file:

0 . 0 6
0 . 0 0
0 . 0 0
- 0 . 0 6
- 1 0 . 5 9
3 8 . 4 9

But I don't want spaces or anything else inside the numbers, I need simply this:

0.06
0.00
0.00
-0.06
-10.59
38.49

But when I try delimiter='' or delimiter=None, I get the error "delimiter must be set". How can I write my numbers without delimiters? Maybe it is a very basic question but I can't find it in google. Thank you!

Was it helpful?

Solution

writerow expects an iterable, each element of which will be written to the file, separated by the delimiter. Hence when you give it a string (which itself is an iterable), it writes each character to the file, separated by the delimiter.
What you want to do instead, is to supply the "row" as a list of strings. In your case, each row has only one string, so supply each row as a list with only one string.

The CSV format requires a delimiter of some sort. Classically, this delimiter is the comma - hence the name (CSV = Comma Separated Values). However, should you feel the need to use a different delimiter, you could of course do so (typical choices include space, tab, hyphens, etc)

import csv
export=open('./images2/test.tfw', "wb")
writer=csv.writer(export, delimiter=',', quoting=csv.QUOTE_NONE)
writer.writerow(['0.06'])
writer.writerow(['0.00'])
writer.writerow(['0.00'])
writer.writerow(['-0.06'])
writer.writerow(['-10.59'])
writer.writerow(['38.49'])
export.close()

OTHER TIPS

As it has been said in the comments, you don't even need to use the csv module. A more simple solution:

with open('./images2/test.tfw', 'w') as export:
    export.write('0.06\n')
    export.write('0.00\n')
    export.write('0.00\n')
    export.write('-0.06\n')
    export.write('-10.59\n')
    export.write('38.49')

"No delimiter", you say? Other than the new-line, I presume. This is just writing lines of text in Python...

data = (0.06, 0.00, 0.00, -0.06, -10.59, 38.49,)
with open('./images2/test.tfw', 'w') as export:
    for line in data:
        export.write('{}\n'.format(line))
import csv
export=open('./images2/test.tfw', "wb")
writer=csv.writer(export, delimiter='', quoting=csv.QUOTE_NONE)
writer.writerow(['0.06'])
writer.writerow(['0.00'])
writer.writerow(['0.00'])
writer.writerow(['-0.06'])
writer.writerow(['-10.59'])
writer.writerow(['38.49'])
export.close()

I've not worked much with csvwriter, but providing nothing between quotes as above, or perhaps NUL without quotes may do the trick? This is considering you want 0 delimitation in your CSV.

If your looking to simply write text, the following may suit your purposes. No need for csvwriter if your not looking to use CSV delimitation.

a = open('./images2/test.tfw', "w")
a.write('0.06\n')
a.write'0.00\n')
a.write('0.00\n')
a.write('-0.06\n')
a.write('-10.59\n')
a.write('38.49\n')
a.close()

You could do it like this to get around the problem that strings are sequences:

import csv

def write_string(writer, s):
    return writer.writerow((s,))

with open('test.tfw', "wb") as export:
    writer = csv.writer(export, quoting=csv.QUOTE_NONE)
    write_string(writer, '0.06')
    write_string(writer, '0.00')
    write_string(writer, '0.00')
    write_string(writer, '-0.06')
    write_string(writer, '-10.59')
    write_string(writer, '38.49')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top