我在保存一个csv文件时遇到了一些困难,这个csv文件应该包含一个由两个字符串组成的字符串中的多行,保存时,文件会将最后一行“打断”为几行,并在文件中的逗号周围加上“,”(如“,”)
如有任何帮助,我将不胜感激
谢谢
我尝试过用各种方法编写文件,例如 1.for循环写入文件 2.将代码中的“with open”和“with open”合并为1 3.打开一个文件,用“wb”(字节)读取文件
list1 = []
list2 = []
with open(updated_file+'.txt', 'r+', encoding='utf-8') as csv_file:
for row in csv_file:
list1.append(row.split()[0])
not_seperated = (row[:6])
seperated = (([not_seperated[i:i + 2] for i in range(0, len(not_seperated), 2)]))
seperator = "-"
joined = seperator.join(seperated)
print(joined)
with open(updated_file + '.txt', 'r+', encoding='utf-8') as csv_file:
for row in csv_file:
list2.append(row.split()[0])
name = (row[6:])
print(name)
joinedlist = joined + name
print(joinedlist)
writer = csv.writer(csv_file)
writer.writerows(joinedlist)
预期结果应为CSV文件,其中包含以下内容: 12-34-56,姓名(新行) 10-20-30,另一个人的名字(新行) ....
目前没有回答
相关问题 更多 >
编程相关推荐