用文本替换csv中的值,并将其写入文件和中心段落

时间:2013-03-30 00:00:33

标签: python csv python-2.7 replace

注意:原始代码已更改以反映建议。对于像我这样的新手来说,保留原帖几乎完好无损。

我有一个csv文件,它可以包含从零到最多4个这些值。

COLOUR,Code1,Code2  
Red,1,A  
White,3,D  
Blue,1,C  
Orange,2,D  

我有一个测试文件(test_test.txt),其中包含以下内容:

"Colours have come to symbolize many things. "

我在努力的地方不知道如何更换价值观。我想搜索颜色文件,如果找到值,请将其替换为另一个值:

"Red is the colour of blood, rubies, and strawberries." replaces Red  
"WhitePages is the world's largest and most trusted source for business." replaces White  
"The term blue moon is used colloquially to mean a rare event." replaces Blue  
"Orange you glad I have no more colours?" replaces Orange 

我想将它写成两种格式的文本文件(段落和列表):

Red is the colour of blood, rubies, and strawberries, WhitePages is the world's largest and most trusted source for business, The term "blue moon" is used colloquially to mean a rare event, and Orange you glad I have no more colours?

我希望能够将其集中在文本文档中:

Red is the colour of blood, rubies, and strawberries  
WhitePages is the world's largest and most trusted source for business  
The term "blue moon" is used colloquially to mean a rare event  
Orange you glad I have no more colours?  

以下是包含建议的修订代码。任何关于如何将最后4个句子集中在任何人的想法?

import csv

with open('C:/Test/test_text.txt', 'a') as f:
    with open('C:/Test/colour.csv', 'rb') as test:
        colour_dict = {
            'Red':    "Red is the colour of blood, rubies, and strawberries",
            'White':  "WhitePages is the world's largest and most trusted source for business",
            'Blue':   "The term blue moon is used colloquially to mean a rare event",
            'Orange': "Orange you glad I have no more colours?"
        }
        reader = csv.DictReader(test)
        colour_list = tuple([colour_dict[row["COLOUR"]] for row in reader])

        # Wtite out comma separated list.

        if len(colour_list) == 0:
            colourGroup =  exit
        elif len(colour_list) == 1:
            colourGroup =  '%s' % colour_list #

        elif len(colour_list) == 2:
            colourGroup =  '%s and %s' % colour_list

        else:
            colourGroup = ('%s, ' * (len(colour_list)-1) + 'and %s') % tuple(colour_list)
        f.write(colourGroup)

        # Write  out list as separate lines.
        f.write ('\n\ncolour can influence our emotions, our actions and how we respond to various people, things and ideas. Much has been studied and written about colour and its impact on our daily lives.\n\nMany people believe that colours are powers, and that bright colours are especially powerful. Here are some of the meanings of colours and the energies contained in their corresponding stones.')
        f.write('\n\n' + ('\n'.join(colour_list)))

任何和所有帮助都非常感激。

1 个答案:

答案 0 :(得分:1)

您可以使用字典将颜色转换为所需的替换颜色:

colour_dict = {
    'Red':    "Red is the colour of blood, rubies, and strawberries",
    'White':  "WhitePages is the world's largest and most trusted source for business",
    'Blue':   "The term blue moon is used colloquially to mean a rare event",
    'Orange': "Orange you glad I have no more colours?"
}
colour_list = tuple([colour_dict[row["COLOUR"]] for row in reader])

现在,colour_list包含用其表达式替换的颜色。如果csv包含意外值,则应处理异常。

虽然您的问题中未提及,但您的目标似乎是将结果导入Word文档。如果你想这样做,并且你的文本真正居中,我认为最好的方法是生成一个RTF格式文件而不是纯文本文件。这支持真正的居中,可以通过Word阅读。

我上传了an example,也许这个simple tutorialRTF specification可以为您提供指导。或者也许使用Word的XML格式也可以,但我没有经验。