我正在將一行一行寫入外部文件。每行有9個由Tab分隔符分隔的列。如果我在該文件中分割每行並輸出最後一列,我可以看到\ n被添加到9列的末尾。我的代碼是: n在每行尾附加
#!/usr/bin/python
with open("temp", "r") as f:
for lines in f:
hashes = lines.split("\t")
print hashes[8]
最後一列的值是整數,1或2。當我運行這個程序,輸出我得到的是,
['1\n']
['2\n']
我應該只得到1或2。爲什麼在這裏附加'\ n'?
我嘗試了以下檢查以消除該問題。
with open("temp", "r") as f:
for lines in f:
if lines != '\n':
hashes = lines.split("\t")
print hashes[8]
這也不起作用。我試過if lines != ' '
。我怎樣才能讓它消失?提前致謝。
這將修改一些柱狀數據。 ''\ n''是單個字符。所以'哈希[8] [: - 1]'是我認爲你在 – inspectorG4dget