[英]How to write a text file with no any delimiter in python?
I wish to write a simple text file with 6 lines in Python 2.7.我想用 Python 2.7 编写一个 6 行的简单文本文件。 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" .但是当我尝试
delimiter=''
或delimiter=None
,我收到错误“必须设置分隔符” 。 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!
谢谢!
writerow
expects an iterable, each element of which will be written to the file, separated by the delimiter. writerow
需要一个可迭代的,其中的每个元素都将写入文件,由分隔符分隔。 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. CSV 格式需要某种分隔符。 Classically, this delimiter is the comma - hence the name (CSV = Comma Separated Values).
传统上,此分隔符是逗号 - 因此得名(CSV = 逗号分隔值)。 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()
As it has been said in the comments, you don't even need to use the csv
module.正如评论中所说,您甚至不需要使用
csv
模块。 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 ...
这只是在 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?我与 csvwriter 的合作不多,但是在上面的引号之间什么也不提供,或者没有引号的 NUL 可能会起作用? This is considering you want 0 delimitation in your CSV.
这是考虑到您希望在 CSV 中使用 0 分隔符。
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.
如果您不想使用 CSV 分隔,则不需要 csvwriter。
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')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.