[英]How to join list of strings and write to csv file?
I have a list containing source code lines as follows:我有一个包含源代码行的列表,如下所示:
item1: with open('a.txt', 'a') as fd:
item2: fd.write(a)
I want to join them together like:我想像这样将它们加入到一起:
with open('a.txt', 'a') as fd\nfd.write(a)
And write this line into a csv column.并将此行写入 csv 列。 But when I try to write it, the writer itself separate each line by new line character and saves as:
但是当我尝试写它时,作者自己用换行符分隔每一行并另存为:
with open('a.txt', 'a') as fd
fd.write(a)
Here is my code:这是我的代码:
column_2 = [with open('a.txt', 'a') as fd: , fd.write(a)]
column_2 = [with open('a.txt', 'a') as fd: , fd.write(a)]
with open('./dataset/data.csv', 'a', ) as fd:
field_names = ['column 1', 'column 2']
writer_object = writer(fd)
a = '\n'.join(column_1)
b = '\n'.join(column_2)
my_data = [a, b]
writer_object.writerow(my_data)
Thanks.谢谢。
Escape the backslash ie \\n
to prevent the character being treated as a newline.转义反斜杠,即
\\n
以防止字符被视为换行符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.