views:

86

answers:

7

I'm trying to get Python to a read line from a .txt file and write the elements of the first line into a list.

The elements in the file were separated with a TAB so I used split("\t") to separate the elements.

Because the .txt file has a lot of elements I saved the data found in each line into a separate list

(data from the first line into list1 and so on....)


NOW THE QUESTION:

The problem I currently have is that it's showing each list like this:

['Name1', '7.3', '6.9', '6.6', '6.6', '6.1', '6.4', '7.3\n']

What do i have to do to remove \n from the last element of the list and make it just '7.3'?

+4  A: 

If you want to remove \n from the last element only, use this:

t[-1] = t[-1].strip()

If you want to remove \n from all the elements, use this:

t = map(lambda s: s.strip(), t)

You might also consider removing \n before splitting the line:

line = line.strip()
# split line...
Bolo
I would do a strip before splitting...i feel its more concise.
st0le
Huzzah! It Works! Thank you very much.
Mr Wotan
if I was going to iterate over the list, I would use `[s.strip() for s in t]` as well. I timed it and it's 5.33 msec to process `["s\n"]*10000` vs. 9.73 msec for `map`. `map` will win if it's mapping a builtin.
aaronasterling
@Aaron Thanks, I'll keep that in mind!
Bolo
`line = line.strip()` will remove ALL TRAILING WHITESPACE. This is butchery. Read Jim Dennis's answer.
John Machin
@John True. Not only that, it'll remove all leading whitespace as well. In many circumstances `line.strip()` would be more reasonable than `line.rstrip('\n')` and that's why I wrote it without further explanation. However, in this case (tab-delimited values) you're 100% right: one should indeed be careful with removing leading and trailing whitespace, since an empty first or last column might "disappear".
Bolo
A: 

You access the last element of the set and then store the value in a variable.

So you have:

fileName = '7.3\n'

then just do:

fileName.strip()

which will leave you with 7.3

then store that value back in the last element of the set

you can use lstrip() or rstrip() to remove just the left or right side.

Hope this helps. If you need any more help let me know

PK

Pavan
+1  A: 

As an alternate method, if you know that there are no spaces in your data, which it seems is the case, you can use split() (with no arguments). This splits on white space and uses a more efficient algorithm than the other version of split. It also strips whitespace from both ends.

line = line.split()

And that's it.

JoshD
A: 

Using list comprehension:

myList = ['Name1', '7.3', '6.9', '6.6', '6.6', '6.1', '6.4', '7.3\n']

[(el.strip()) for el in myList]
rogeriopvl
A: 

You could do -

DELIMITER = '\t'
lines = list()
for line in open('file.txt'):
    lines.append(line.strip().split(DELIMITER))

The lines has got all the contents of your file.

One could also use list comprehensions to make this more compact.

lines = [ line.strip().split(DELIMITER) for line in open('file.txt')]
MovieYoda
+1  A: 

str.strip() removes the whitespace characters. you can also pass custom characters as argument to strip. The strip function removes the whitespace/custom characters on both ends of the string. lstrip() and rstrip() are left strip and right strip functions resp.

Eg:

test_str = "Vishaka\n"

test_str = test_str.strip()

test_str's now Vishaka

gopalkoduri
+1  A: 

It sounds like you want something like the Perl chomp() function.

That's trivial to do in Python:

def chomp(s):
    return s[:-1] if s.endswith('\n') else s

... assuming you're using Python 2.6 or later. Otherwise just use the slightly more verbose:

def chomp(s):
    if s.endwith('\n'):
        return s[:-1]
    else:
        return s

If you want to remove all new lines from the end of a string (in the odd case where one might have multiple trailing newlines for some reason):

def chomps(s):
    return s.rstrip('\n')

Obviously you should never see such a string returned by any normal Python file object's readline() nor readlines() methods.

I've seen people blindly remove the last characters (using s[:-1] slicing) from the results of file readline() and similar functions. This is a bad idea because it can lead to an error on the last line of the file (in the case where a file ends with anything other than a newline).

At first you might be lulled into a false sense of security when blindly stripping final characters off lines you've read. If you use a normal text editor to create your test suite files you'll have a newline silently added to the end of the last line by most of them. To create a valid test file use code something like:

f = open('sometest.txt', 'w')
f.write('some text')
f.close()

... and then if you re-open that file and use the readline() or readlines() file methods on it you'll find that the text is read without the trailing newline.

This failure to account for text files ending in non-newline characters has plagued many UNIX utilities and scripting languages for many years. It's a stupid corner base bug that creeps into code just often enough to be a pest but not often enough for people to learn from it. We could argue that "text" files without the ultimate newline are "corrupt" or non-standard; and that may be valid for some programming specifications.

However, it's all too easy to ignore corner cases in our coding and have that ignorance bite people who are depending on your code later. As my wife says: when it comes to programming ... practice safe hex!

Jim Dennis
+1 When reading a Python text file, checking for a presence of a newline (or blindly removing it if it exists by `line = line.rstrip('\n')`) should be done as a separate step BEFORE parsing the line into fields.
John Machin