python - Remove a column of text -


i'm new python, have scripting experience in other languages. i'm trying write script reads text file data formatted this:

1    52345 2    53 3    -654 4    2342 

and print out this:

52345, 53, -654, 2342, 

so want remove first column , whitespace , add comma @ end.

this have far, seems i'm trying use string methods on file object.

def remove_first_column(text):     splitstring = text.split(' ')      splitstring.pop(0)       finalstring = "  ".join(         [item.strip() item in splitstring])      return finalstring  def main():     print "type filename:"     file_again = raw_input("> ")     txt = open(file_again)     line in txt.split("\n"):         print(remove_first_column(line))  #py2.5  print remove_first_column(line)  if __name__ == '__main__':     main() 

i exception saying "attributeerror: 'file' object has no attribute 'split'

yes, when open file using open function, returns file object, can iterate on file object (to each line) ,but cannot use file.split('\n') , etc.

try doing -

for line in txt:     print(remove_first_column(line))  #py2.5  print remove_first_column(line) 

you not need split , iterating on txt file return each line in each iteration.

also, inside function, doing - splitstring = text.split(' ') , if string has multiple spaces in between 2 columns, can lead many empty string elements - ['1', '', '', '', '52345'] (though not cause issue in particular code) , letting know , can - splitstring = text.split() , split whitespaces , (it result in list - ['1','52345']


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -