Python skip first line .readlines( )[1:] not working? - python

If I remove [1:], if works fine and prints all data.
f = open("test.csv", "r")
lines = f.readlines()
f.close()
print lines
result:
['title1,title2\raa,aaaa\rbb,bbbb\rcc,cccc']
but if I try to skip the first line by adding [1:]
f = open("test.csv", "r")
lines = f.readlines()[1:]
f.close()
print lines
it prints an empty array
[]
I'm using python 2.7.6.
Does anyone know why?

result:
['title1,title2\raa,aaaa\rbb,bbbb\rcc,cccc']
but if I try to skip the first line by adding [1:] it prints empty array
It looks like you have a platform line encoding issue. You're assuming that python reads this as a multi-line file; however, python only sees one line.
Modify your code to do this...
f = open("test.csv", "r")
lines = f.read().splitlines() # Thanks to Ashwini's comment for tip
f.close()
print lines

Related

How can I replace first line of a csv with a header using Python?

I'm trying to replace the first line of a csv input file with a header. The first line is blank, 0. See image below.
I want the blank and 0 to be "ID" and "sil_score" respectively. See below:
But I keep getting this:
import csv
r = csv.reader(open('C:/Users/Desktop/Geosill/attempt1.csv'))
lines = list(r)
lines[1][0] = 'ID'
lines[2][0] = 'sil_score'
writer = csv.writer(open('C:/Users/Desktop/Geosill/attempt3.csv', 'w'))
writer.writerows(lines)
This will do it. newline='' should be used to fix the blank line issue you are seeing as well.
import csv
with open('input.csv',newline='') as f:
r = csv.reader(f)
lines = list(r)
lines[0] = ['ID','sil_score']
with open('output.csv','w',newline='') as f:
w = csv.writer(f)
w.writerows(lines)
If you're looking to edit the first two lines of the .csv you'll have to change how you access the lines list.
You'll need to use
lines[0][0]='ID'
lines[0][1]='sil_score'
instead.
The output seems odd though, could be something weird with the csv import. Try opening the files in a text editor, might be easier to see what's going on.
You can do this without using csv.writer. Try this:
with open('C:/Users/Desktop/Geosill/attempt1.csv', "r") as infile:
lines = infile.readlines().rstrip().split(",")
lines[0] = ["ID", "sil_score"]
with open('C:/Users/Desktop/Geosill/attempt1.csv', "w") as outfile:
for line in lines:
outfile.write(",".join(line))
Hope this helps!

Deleting line from file by containing a specific string is not working in python

I'm trying to delete each line which contains "annote = {" but my code is not working.
I have a file stored in a variable myFile and I want to go through every line of this file and delete every line which contains the string annote.
this is basically my code:
print(myFile.read()) //prints myFile
myFile.seek(0)
for lines in myFile:
if b"annote = {" in lines:
lines = lines.replace(b'.', b'')
myFile.seek(0)
print(myFile.read()) //this prints the exact same as the print method above so annote lines
//haven't been removed from this file
I have no idea why annote lines doesn't get removed. There is probably anything wrong with the replace method because it always is inside the if request but nothing happens with annote lines. I've also tried lines.replace(b'.', b'') instead of lines = lines.replace(b'.', b'') but nothing happened.
Hope anyone can help me with this problem
This will do it for you.
f.readlines() returns a list of text lines
Then you check for the lines that do not contain the things you do not want
Then you write them to a separate new file.
f2 = open('newtextfile.txt', 'w')
with open('text_remove_line.txt', 'r') as f:
for line in f.readlines():
if 'annote = {' not in line:
f2.write(line)
f2.close()
This should work:
with open('input.txt') as fin :
lines = fin.read().split('\n') # read the text, split into the lines
with open('output.txt', 'w') as fout :
# write out only the lines that does not contain the text 'annote = {'
fout.write( '\n'.join( [i for i in lines if i.find('annote = {') == -1] ))

skip 2 lines after a match while reading from a file

I am trying to code something where I first look for some string in a line in a txt file and when it is found I want to skip that row and the row below to get a new txt file without those rows. I really didn't get any solution from other questions here so maybe this will work
My code looks like this now:
with open("bla.txt", "r+") as f
new_f = f.readlines()
f.seek(0)
for line in new_f:
if "abc" not in line:
f.write(line)
else:
pass
pass
f.truncate()
I tried it with next(f) aswell but it didn't work for me. thanks in advance
This code creates a new file that skip the current and next row if the current row has the string ABC:
with open('bla.txt','r') as f:
text = f.read()
lines = text.split('\n')
with open('new_file.txt','w') as nf:
l = 0
while l<(len(lines)):
if 'ABC' in lines[l]:
l = l+2
else:
nf.write(lines[l]+'\n')
l = l+1
Try something simple like this:
import os
search_for = 'abc'
with open('input.txt') as f, open('output.txt', 'w') as o:
for line in f:
if search_for in line:
next(f) # we need to skip the next line
# since we are already processing
# the line with the string
# in effect, it skips two lines
else:
o.write(line)
os.rename('output.txt', 'input.txt')
Here is a repl with sample code.

Python read() works with UTF-8 but readlines() "doesn't"

So, I am working with a (huge) UTF-8 encoded file. The first thing I do with it it's get it's lines in a list using the File Object readlines() method. However when I use the print command for debugging I get things like, for example, \xc3 etc.
Here's a really small example that replicates my problem; I created a t.txt file that contains only the text "Clara Martínez"
f = open("t.txt", "r")
s = f.read()
print s
Clara Martínez
#If I do the following however
lines = f.readlines()
for l in lines:
print l
['Clara Mart\xc3\xadnez']
#write however works fine!
f2 = open("t2.txt", "w")
for l in lines:
f2.write(l)
f2.close()
f1.close()
And then I open the "t2.txt", the string is correct, i.e: Clara Martínez.
Is there any way to "make" readlines() work as read()?
You claim that this:
lines = f.readlines()
for l in lines:
print l
Will result in this:
['Clara Mart\xc3\xadnez']
This is not true, it will not. I think you made a mistake in your code, and wrote this:
lines = f.readlines()
for l in lines:
print lines
That code will give the result you say, assuming the file contains only one line with the text 'Clara Mart\xc3\xadnez'.

Copy the last three lines of a text file in python?

I'm new to python and the way it handles variables and arrays of variables in lists is quite alien to me. I would normally read a text file into a vector and then copy the last three into a new array/vector by determining the size of the vector and then looping with a for loop a copy function for the last size-three into a new array.
I don't understand how for loops work in python so I can't do that.
so far I have:
#read text file into line list
numberOfLinesInChat = 3
text_file = open("Output.txt", "r")
lines = text_file.readlines()
text_file.close()
writeLines = []
if len(lines) > numberOfLinesInChat:
i = 0
while ((numberOfLinesInChat-i) >= 0):
writeLine[i] = lines[(len(lines)-(numberOfLinesInChat-i))]
i+= 1
#write what people say to text file
text_file = open("Output.txt", "w")
text_file.write(writeLines)
text_file.close()
To get the last three lines of a file efficiently, use deque:
from collections import deque
with open('somefile') as fin:
last3 = deque(fin, 3)
This saves reading the whole file into memory to slice off what you didn't actually want.
To reflect your comment - your complete code would be:
from collections import deque
with open('somefile') as fin, open('outputfile', 'w') as fout:
fout.writelines(deque(fin, 3))
As long as you're ok to hold all of the file lines in memory, you can slice the list of lines to get the last x items. See http://docs.python.org/2/tutorial/introduction.html and search for 'slice notation'.
def get_chat_lines(file_path, num_chat_lines):
with open(file_path) as src:
lines = src.readlines()
return lines[-num_chat_lines:]
>>> lines = get_chat_lines('Output.txt', 3)
>>> print(lines)
... ['line n-3\n', 'line n-2\n', 'line n-1']
First to answer your question, my guress is that you had an index error you should replace the line writeLine[i] with writeLine.append( ). After that, you should also do a loop to write the output :
text_file = open("Output.txt", "w")
for row in writeLine :
text_file.write(row)
text_file.close()
May I suggest a more pythonic way to write this ? It would be as follow :
with open("Input.txt") as f_in, open("Output.txt", "w") as f_out :
for row in f_in.readlines()[-3:] :
f_out.write(row)
A possible solution:
lines = [ l for l in open("Output.txt")]
file = open('Output.txt', 'w')
file.write(lines[-3:0])
file.close()
This might be a little clearer if you do not know python syntax.
lst_lines = lines.split()
This will create a list containing all the lines in the text file.
Then for the last line you can do:
last = lst_lines[-1]
secondLAst = lst_lines[-2]
etc... list and string indexes can be reached from the end with the '-'.
or you can loop through them and print specific ones using:
start = start line, stop = where to end, step = what to increment by.
for i in range(start, stop-1, step):
string = lst_lines[i]
then just write them to a file.

Categories