我有一个电子表格(.ods格式),如下所示:
Column A Column B
abstemious marked by restraint
ameliorate to make better or more tolerable
amphora an ancient jar with 2 handles on the top
如何将其转换为具有以下格式的文本文件?
Q: abstemious
A: marked by restraint
Q: ameliorate
A: to make better or more tolerable
Q: amphora
A: an ancient jar with 2 handles on the top
注意Q:,A:和新行。我搜索了Google并堆栈溢出并尝试了很多东西,包括转换为.csv作为中间步骤并使用“查找和替换”,在电子表格中创建带有“\ n”的列,还使用“#13#10”表示一个新行但它似乎没有转换为最终的.txt文件。
另外,为了格式化这篇文章,我将所有内容缩进4个空格并手动输入,是否有更好的方法来模拟堆栈溢出时的电子表格(例如,如果我要发布更复杂的内容)?我看了http://daringfireball.net/projects/markdown/syntax,但如果我想发布包含多个列和行的电子表格的一部分,我不知道该怎么做。我搜索了这个主题,也没有找到任何东西。谢谢!
答案 0 :(得分:0)
可能有一种更容易的方法,但我想出了一种方法。首先,使用未出现在文件中的符号(例如,'$')作为字段分隔符,并将.ods转换为.csv。接下来我用Python编写了这段代码:
def convert():
f = open(r'name_of_file.csv','r')
contents = f.read()
contents = contents.split('\n')
contents.pop()
p = []
for element in contents:
p.append(element.split('$'))
for element in p:
print('Q: ' + element[0])
print('A: ' + element[1])
print('')
没有理由发布其他答案,因为现在编写该程序就足够了。