I'm completely new to Python, but I have some scripting experience in other languages. I'm trying to write a script that reads a text file with data formatted like this:
1 52345
2 53
3 -654
4 2342
and print it out like this:
52345,
53,
-654,
2342,
So I want to remove the first column and whitespace and add a comma at the end.
This is what I have so far, but it seems like I'm trying to use string methods on a file object.
def remove_first_column(text):
splitString = text.split(' ')
splitString.pop(0)
finalString = " ".join(
[item.strip() for item in splitString])
return finalString
def main():
print "Type the filename:"
file_again = raw_input("> ")
txt = open(file_again)
for line in txt.split("\n"):
print(remove_first_column(line)) #py2.5 print remove_first_column(line)
if __name__ == '__main__':
main()
I get an exception saying that "AttributeError: 'file' object has no attribute 'split'