将text,csv,excel文件放入模式中

时间:2013-08-18 22:19:49

标签: c++ python g++-4.7

我是真正编程的初学者并且遇到了问题  我想读取存储在文件/ csv / txt / excel中的许多实例 喜欢下面的

find<S>ing<G>s<p> 

然后,当我读到这个文件时,它会遍历每个字符并从六个位置开始并一直持续到11个位置 - 单个行的最大大小为12

            -,-,-,-,-,f,i,n,d,i,n,0
            -,-,-,-,f,i,n,d,i,n,g,0
            -,-,-,f,i,n,d,i,n,g,s,0
            -,-,f,i,n,d,i,n,g,s,-,S//there is an S value next to the letter d
            -,f,i,n,d,i,n,g,s,-,-,0
            f,i,n,d,i,n,g,s,-,-,-,0
            i,n,d,i,n,g,s,-,-,-,-,G // there is a G value here at th end of g 
            n,d,i,n,g,s,-,-,-,-,-,P */// there is a P value here at th end of s 

这是我在python中尝试过的代码。但可以在c ++,java,dotNet中使用。

import sys
import os
f = open('/home/mm/exprimentdata/sample3.csv')// can be txt file 
string = f.read()

a = [] 
b = []
i = 0
while (i < len(string)):

    if (string[i] != '\n '):
        n = string[i]
        if (string[i] == ""):
            print ' = '
                if (string[i] = upper | numeric)
                print rep(char).rjust(12),delimiter=','
        a.append(n)

    i = (i+1)


print (len(a))
print a

我的问题是如何比较每个字符串并在最右边的部分分配一个字符(位置12,如上面的G,P,S)

如何在对齐第一行后向后退一步?

我该如何修复长度

请任何人看到片段并调整以解决上述情况

1 个答案:

答案 0 :(得分:-1)

我不明白你的问题。

但有些建议:

首先,您应该在打开文件后关闭该文件。

f = open('/home/mm/exprimentdata/sample3.csv')// can be txt file 
string = f.read()
**f.close()**

其次,你的缩进是有问题的。空白在Python中很重要。 (也许你的真实代码是正确缩进的,它只是一个StackOverflow的东西。)

第三,不应该使用while循环和递增,而应该写:

for i range(len(string)):
    # loop code

第四,这一行永远不会评估为True:

if (string[i] == ""):

string [i]将始终是某个字符(或导致越界错误)。

我建议您在尝试编写此程序之前阅读Python教程。

相关问题