[Solved]dealing with columns

This is the place for queries that don't fit in any of the other categories.

[Solved]dealing with columns

Postby bfantinatti » Mon Nov 25, 2013 7:24 pm

Hello guys, I am new on this forum, and new in python coding.
I have a problem to solve, related to python classes.
I am trying to:
1- read a file with some columns
2- select specific columns (ex. columns 1, 3 and 5)
3- print those columns in a unique file, in a intercalary form.

Example
- Input:
col1 col2 col3 col4 col5
A1 A2 A3 A4 A5
B1 B2 B3 B4 B5
C1 C2 C3 C4 C5

- Selecting cols 1, 3 and 5

- Final output:
A1
A3
A5
B1
B3
B5
C1
C3
C5

I would like to say that I ve done this using bash, but for learning purposes, I need to do using python.
Thank you guys!
Last edited by stranac on Wed Dec 04, 2013 3:22 pm, edited 2 times in total.
Reason: First post lock. Marked as solved.
bfantinatti
 
Posts: 7
Joined: Mon Nov 25, 2013 7:15 pm

Re: dealing with columns

Postby stranac » Mon Nov 25, 2013 7:33 pm

You can use the csv module to make the reading of files easier.
Alternatively, you can use str.split() to split your lines to lists.

After that, you just take the stuff you want and write it to a file however you want.
If you're having trouble implementing this, feel free to post your code(do explain what your problem is, paste a traceback if you get it, and all that good stuff...)
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 896
Joined: Thu Feb 07, 2013 3:42 pm

Re: dealing with columns

Postby bfantinatti » Mon Nov 25, 2013 10:39 pm

Thank you stranac, I will try that, and the I come back with the code.
Thank you very much
bfantinatti
 
Posts: 7
Joined: Mon Nov 25, 2013 7:15 pm

Re: dealing with columns

Postby bfantinatti » Tue Dec 03, 2013 4:50 pm

Hello stranac, all I was abble to do is to import the file in read mode, and import the csv module.
Now, is necessary to manipulate the file, semecting the columns I need, and then go to next step.
Is this code correct for input the file I want?

Code: Select all
#!/usr/bin/python
import csv
arq = raw_input("Insert the file name: ")
arq = open(arq, 'r')


Thank you!
bfantinatti
 
Posts: 7
Joined: Mon Nov 25, 2013 7:15 pm

Re: dealing with columns

Postby stranac » Tue Dec 03, 2013 4:58 pm

Well, it's better to use:
Code: Select all
with open(arq, 'r') as arq:
    # use the file here

And you shouldn't be reusing names like that(arq representing two different things), it's confusing.
Other than that, your code looks fine so far.

Next, you'll need to create a csv.reader object, and loop over it's rows using a for loop.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 896
Joined: Thu Feb 07, 2013 3:42 pm

Re: dealing with columns

Postby bfantinatti » Wed Dec 04, 2013 11:11 am

In fact, the "arq" that I mean, is the same file variable (Can I do this?)
For example: the file I want to work with is called "example.sam"
And the code I tryed now is:

Code: Select all
import csv
arq = raw_input("Insert the file name: ")
with open(arq, 'r') as arq:
   arq


Then I enter the file name "example.sam". Seems to be working. After I enter the name, the program finishes without any error message. Is that correct?
bfantinatti
 
Posts: 7
Joined: Mon Nov 25, 2013 7:15 pm

Re: dealing with columns

Postby Kebap » Wed Dec 04, 2013 12:04 pm

That may be technically correct, however it is not recommended, like stranac explained. However sometimes I do the same in my prototype codes. Just make sure to use understandable variable names at some point. Otherwise you'll have pretty unmainainable code soon. Did you have any success on using the csv module yet?
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 282
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: dealing with columns

Postby bfantinatti » Wed Dec 04, 2013 3:09 pm

Yes, I got help with some friends here, and the problem is solved.
As I said, I have a file with some columns, and I need to put the lines of specific columns in a new file.
This code worked revy well:

Code: Select all
import csv
z=open('test', 'r')
out=open('result-test','w')
test=z.readlines()
for line in test:
        columns = line.split('\t')
        out.write('\n'.join([columns[0],columns[3]]))


Note that "test" now is my input file, and "result-test" is my output file containing the data results.
Thank you very much guys! :D
bfantinatti
 
Posts: 7
Joined: Mon Nov 25, 2013 7:15 pm

Re: dealing with columns

Postby bfantinatti » Wed Dec 04, 2013 3:13 pm

**this code worked VERY well... (Sorry)

Guys, how can I edit the topic name to add "SOLVED"?
bfantinatti
 
Posts: 7
Joined: Mon Nov 25, 2013 7:15 pm

Re: dealing with columns

Postby stranac » Wed Dec 04, 2013 3:32 pm

I've marked it solved for you.

You don't seem to be using the csv module at all, so there's no need to import it.
I would also like to suggest some changes to your code:
  • use meaningful names(instead of z, test...)
  • use the with statement when opening files to make sure the files are closed after use
  • iterate over the open file to get its lines
Here's what the relevant part of the code would look like if rewritten like that:
Code: Select all
with open('test', 'r') as in_file, open('result-test', 'w') as out_file:
    for line in in_file:
        # do your stuff here
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 896
Joined: Thu Feb 07, 2013 3:42 pm

Re: [Solved]dealing with columns

Postby bfantinatti » Wed Dec 04, 2013 6:15 pm

Thank you very much! :)
bfantinatti
 
Posts: 7
Joined: Mon Nov 25, 2013 7:15 pm


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 5 guests