通过向带空格的字符串添加双引号将值写入 python 中的文件
Write values to a file in python by adding double quotes to the string with spaces
我正在尝试以特定方式在 txt 文件中写入以下列表的值
valList = ['1','Age',' Address has spaces','employee','98363','hobbies has space in it ']
sample.txt
1,Age,"Address has spaces",employee,98363,"hobbies has space in it "
这里如果你看到valList是一个异构列表,我的唯一目的是写出其中有空格并用双引号括起来的值,其余所有值都应该按原样写。
到目前为止我已经尝试过:
valList = ['1','Age',' Address has spaces','employee','98363','hobbies has space in it ']
# Output = 1,Age,"Address has spaces",employee,98363,"hobbies has space in it "
valstr = ",".join(valList)
with open('sample.txt','wb') as file:
file.write(valstr)
file.write('\n' + " \"" + valList[2] + "\" ")
使用 file.write('\n' + " \"" + valList[2] + "\" ") 行我可以写一个带引号的价值,
但是我需要一种通用的方法来为给定的列表写入带有空格的值。
非常感谢任何帮助。
您有 CSV 数据。使用 csv
module 它会在需要的地方自动使用引号:
import csv
valList = ['1', 'Age', ' Address has spaces', 'employee', '98363', 'hobbies has space in it ']
with open('sample.txt', 'wb') as file:
writer = csv.writer(file)
writer.writerow(valList)
这将根据需要引用值;例如,当字段值中有逗号或换行符时。这不太符合您的确切要求,但兼容的 CSV reader 将能够在读取时再次从文件中获取完全相同的值。
如果您必须 在带有 space 的列上加上引号,您可以禁用自动引号并手动添加引号:
import csv
valList = ['1','Age',' Address has spaces','employee','98363','hobbies has space in it ']
with open('sample.txt', 'wb') as file:
writer = csv.writer(file, quoting=csv.QUOTE_NONE, quotechar='')
writer.writerow(['"{}"'.format(v) if ' ' in v else v for v in valList])
列表推导式将值中带有 space 的任何内容用双引号括起来。请注意,因为我们在此处禁用引用,所以 逗号或换行符 的值将被 csv.writer()
拒绝,因为它现在无法引用它们。
两种方法的演示:
>>> import sys
>>> import csv
>>> valList = ['1', 'Age', ' Address has spaces', 'employee', '98363', 'hobbies has space in it ']
>>> writer = csv.writer(sys.stdout)
>>> writer.writerow(valList)
1,Age, Address has spaces,employee,98363,hobbies has space in it
>>> writer = csv.writer(sys.stdout, quoting=csv.QUOTE_NONE, quotechar='')
>>> writer.writerow(['"{}"'.format(v) if ' ' in v else v for v in valList])
1,Age," Address has spaces",employee,98363,"hobbies has space in it "
valList = ['1','Age',' Address has spaces','employee','98363','hobbies has space in it ']
file = open('sample.txt', 'w')
for item in valList:
if ' ' in item:
file.write('"%s",' % item)
else:
file.write("%s," % item)
输出
1,Age," Address has spaces",employee,98363,"hobbies has space in it ",
我正在尝试以特定方式在 txt 文件中写入以下列表的值
valList = ['1','Age',' Address has spaces','employee','98363','hobbies has space in it ']
sample.txt
1,Age,"Address has spaces",employee,98363,"hobbies has space in it "
这里如果你看到valList是一个异构列表,我的唯一目的是写出其中有空格并用双引号括起来的值,其余所有值都应该按原样写。 到目前为止我已经尝试过:
valList = ['1','Age',' Address has spaces','employee','98363','hobbies has space in it ']
# Output = 1,Age,"Address has spaces",employee,98363,"hobbies has space in it "
valstr = ",".join(valList)
with open('sample.txt','wb') as file:
file.write(valstr)
file.write('\n' + " \"" + valList[2] + "\" ")
使用 file.write('\n' + " \"" + valList[2] + "\" ") 行我可以写一个带引号的价值, 但是我需要一种通用的方法来为给定的列表写入带有空格的值。
非常感谢任何帮助。
您有 CSV 数据。使用 csv
module 它会在需要的地方自动使用引号:
import csv
valList = ['1', 'Age', ' Address has spaces', 'employee', '98363', 'hobbies has space in it ']
with open('sample.txt', 'wb') as file:
writer = csv.writer(file)
writer.writerow(valList)
这将根据需要引用值;例如,当字段值中有逗号或换行符时。这不太符合您的确切要求,但兼容的 CSV reader 将能够在读取时再次从文件中获取完全相同的值。
如果您必须 在带有 space 的列上加上引号,您可以禁用自动引号并手动添加引号:
import csv
valList = ['1','Age',' Address has spaces','employee','98363','hobbies has space in it ']
with open('sample.txt', 'wb') as file:
writer = csv.writer(file, quoting=csv.QUOTE_NONE, quotechar='')
writer.writerow(['"{}"'.format(v) if ' ' in v else v for v in valList])
列表推导式将值中带有 space 的任何内容用双引号括起来。请注意,因为我们在此处禁用引用,所以 逗号或换行符 的值将被 csv.writer()
拒绝,因为它现在无法引用它们。
两种方法的演示:
>>> import sys
>>> import csv
>>> valList = ['1', 'Age', ' Address has spaces', 'employee', '98363', 'hobbies has space in it ']
>>> writer = csv.writer(sys.stdout)
>>> writer.writerow(valList)
1,Age, Address has spaces,employee,98363,hobbies has space in it
>>> writer = csv.writer(sys.stdout, quoting=csv.QUOTE_NONE, quotechar='')
>>> writer.writerow(['"{}"'.format(v) if ' ' in v else v for v in valList])
1,Age," Address has spaces",employee,98363,"hobbies has space in it "
valList = ['1','Age',' Address has spaces','employee','98363','hobbies has space in it ']
file = open('sample.txt', 'w')
for item in valList:
if ' ' in item:
file.write('"%s",' % item)
else:
file.write("%s," % item)
输出
1,Age," Address has spaces",employee,98363,"hobbies has space in it ",