1

I have a .csv file with data like this:

uc007ayl.1,ENSMUSG00000041439

uc009mkn.1,ENSMUSG00000031708

uc009mkn.1,ENSMUSG00000035491

I have some codes that read them column by column

    import csv
    import os.path
    #open files + readlines
    with open("C:/Users/Ivan Wong/Desktop/Placement/Lists of targets/Mouse/UCSC to Ensembl.csv", "r") as f:
        reader = csv.reader(f, delimiter = ',')
        #find files with the name in 1st row
        for row in reader:
            graph_filename = os.path.join("C:/Users/Ivan Wong/Desktop/Placement/Interesting reading/3'ORF",row[0]+"_nt_counts.txt.png")
            if os.path.exists(graph_filename):
                y = row[0]+'_nt_counts.txt'  
                r = open('C:/Users/Ivan Wong/Desktop/Placement/fp_mesc_nochx/'+y, 'r')
                k = r.readlines()
                r.close
                del k[:1]
                k = map(lambda s: s.strip(), k)
                interger = map(int, k)   
                import iter

tools
            #adding the numbers for every 3 rows
            def grouper(n, iterable, fillvalue=None):
                "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
                args = [iter(iterable)] * n
                return itertools.izip_longest(*args, fillvalue=fillvalue)
            result = map(sum, grouper(3, interger, 0))
            e = row[1]

Now I can say

print row[1]

to make it show the 2nd column only. I needed to do this because I will be finding these names in another file. But I have a problem because I think python is reading those names in this way:

"E", "N", "S", "M", "U", "S" etc.

This causes a problem now because I won't able to find the match names from another folder. Anyone know where is the problem and how to fix it?

9
  • 1
    Why are you telling csv.reader that the delimiter is ',' when there don't seem to be any commas in your text file? Commented Aug 1, 2012 at 18:26
  • Is your file delimited by ' ' or by ','? Commented Aug 1, 2012 at 18:26
  • @DSM One small mistake, I said its a .txt file but I actually converted it to .csv and they do have a ',' between the 1st and 2nd column Commented Aug 1, 2012 at 18:27
  • @ivanhoifung: I can't reproduce this. You say print row[1] but your code doesn't mention a row. Could you edit your question to include the code you're actually using? [Incidentally, does your filename really have two asterisks in it?] Commented Aug 1, 2012 at 18:32
  • @DSM A bit too complicated for me to understand, I just put the whole codes on see if this work now Commented Aug 1, 2012 at 18:35

1 Answer 1

2

row[1] is not the whole second column of the file. It is just the second field in the current row i.e., just one of many values in the second column.

row[1] is a string in your case. Strings in Python are also sequences; you can call len(some_string) to find out length of string, take an element at a given position pos: some_string[pos], etc.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.