delete first line in a file

J

Juergen Huber

hello,

i am a dummy user in python and new in this programming language and so
there will be questions, that´s for you no problem! i never have before
programmed in any language! sorry for this! i hope you will help me, that i
also could the basics in this language! and later many more, so i hope ! :)

the first question for me in this newsgroup will be the follow one:

- is there a way to delete in a csv-file the first line?!

big thanks, juergen
 
F

Fredrik Lundh

Juergen said:
i am a dummy user in python and new in this programming language and so
there will be questions, that´s for you no problem! i never have before
programmed in any language! sorry for this! i hope you will help me, that i
also could the basics in this language! and later many more, so i hope ! :)

the first question for me in this newsgroup will be the follow one:

- is there a way to delete in a csv-file the first line?!

in general, modern file systems can only remove and add stuff at the end of a
file, so the only way to do this is to rewrite the entire file.

(you can either load the entire file, fix it up, and write it out again, or copy the
file to a new file, skipping the first line or row).

</F>
 
J

Juergen Huber

Fredrik said:
in general, modern file systems can only remove and add stuff at the
end of a
file, so the only way to do this is to rewrite the entire file.

(you can either load the entire file, fix it up, and write it out
again, or copy the file to a new file, skipping the first line or
row).

</F>

ok...i thought as much, that i have to copy this file!

how will i do that?!
how will i fix this file => delete the first line?!

with which commands could i do that?!

thanks
 
J

Juergen Huber

Fredrik said:

that documentation i have already read, but it wouldn`t help me for my
problem!
i know, how i can read the first line an print them on the screen!
but...how can i say python, delete the first line?!
thats my problem!
in the entry of my posting i wrote, that i am a newbie and so please
understand me, that i ask so questions?! :)

thanks for your help!
 
M

Michele Petrazzo

F

Fredrik Lundh

Juergen said:
that documentation i have already read, but it wouldn`t help me for my
problem!

that documentation and a little experimentation should be all you need.
i know, how i can read the first line an print them on the screen!
but...how can i say python, delete the first line?!
thats my problem!

read the first line, and ignore it.

then read all the other lines, and write them to a new file.

</F>
 
?

=?windows-1252?Q?Sch=FCle_Daniel?=

Juergen said:
that documentation i have already read, but it wouldn`t help me for my
problem!
i know, how i can read the first line an print them on the screen!
but...how can i say python, delete the first line?!
thats my problem!
in the entry of my posting i wrote, that i am a newbie and so please
understand me, that i ask so questions?! :)

thanks for your help!


simple!

f = file("old.file")
ignore = f.readline()
file("new.file", "w+").write(f.read())

hth, Daniel
 
J

Juergen Huber

this helps me!

thank you very much!!!!!

greetings, juergen




JH> that documentation i have already read, but it wouldn`t help me for my

JH> problem!

JH> i know, how i can read the first line an print them on the screen!

JH> but...how can i say python, delete the first line?!

JH> thats my problem!

as Fredrik told you, you can not delete the first line directly!!!

JH> in the entry of my posting i wrote, that i am a newbie and so please

JH> understand me, that i ask so questions?! :)

We are trying to understand that, but it is expected some effort on

your side as well :)

You didn't read the documentation properly!

try to experiment with the code by yourself as well!

Try:

1) open your file

2) use readlines() method

which returns the list of ALL rows from the file and store them to some
variable.

it can be done for example like:

all_lines = f.readlines()

(if you do not know, what the list is, start here:

http://docs.python.org/tut/node5.html#SECTION005140000000000000000)

4) save the list members without its first member to the file

for example:

for line in all_lines[1:]:

f.write(line)

5) close the file



Good luck

Petr Jakes











JH> thanks for your help!
 
?

=?windows-1250?Q?Petr_Jake=9A?=

JH> this helps me!

JH> thank you very much!!!!!

JH> greetings, juergen
I am happy to see my postings (which I sent by accident to you only, not to
the list, see below) helped. But....:
Its a pity we can not see your solution (your code) here :(
It helps to others as well to see, how to solve some "problems"

Petr Jakes
JH>> that documentation i have already read, but it wouldn`t help me for my
JH>> problem!
JH>> i know, how i can read the first line an print them on the screen!
JH>> but...how can i say python, delete the first line?!
JH>> thats my problem!
PJ> as Fredrik told you, you can not delete the first line directly!!!
JH>> in the entry of my posting i wrote, that i am a newbie and so please
JH>> understand me, that i ask so questions?! :)
PJ> We are trying to understand that, but it is expected some effort on
PJ> your side as well :)
PJ> You didn't read the documentation properly!
PJ> try to experiment with the code by yourself as well!

PJ> Try:

PJ> 1) open your file
PJ> 2) use readlines() method
PJ> which returns the list of ALL rows from the file and store them to some
PJ> variable.
PJ> it can be done for example like:

PJ> all_lines = f.readlines()

PJ> (if you do not know, what the list is, start here:
PJ> http://docs.python.org/tut/node5.html#SECTION005140000000000000000)

PJ> 4) save the list members without its first member to the file
PJ> for example:

PJ> for line in all_lines[1:]:
PJ> f.write(line)

PJ> 5) close the file

JH> Good luck
JH> Petr Jakes











JH>> thanks for your help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top