(1) Create a file blowing in the Wind.txt, the content is: #问题非原创
How many roads must a man walk down
Before they call him a mans
How many seas must a white dove sail
Before she sleeps in the sand
How many times must the cannon balls fly
Before they ' re forever banned
The answer my friend is blowing
The answer is blowing
(2) Insert the song name "Blowin ' in the Wind" at the head of the file
(3) Insert the singer's name after the song name "Bob Dylan"
(4) Add the string "1962 by Warner Bros. Inc" At the end of the file.
>>> f = open (R ' blowing in the Wind.txt ', ' W ') >>> f.write (' What many roads must a man walk down\nbefore the Y call him a man\nhow many seas must a white Dove Sail\nbefore she sleeps in the sand\nhow many times must the cannon ball s Fly\nbefore they\ ' re forever banned\nthe answer my friend is blowing in the wind\nthe answer was blowing in the wind ') 278 >>> f.close () >>> f = open (R ' blowing in the Wind.txt ') >>> line = F.readlines () # Reads the data one line at a time from the source file and returns it as a list. >>> Line.insert (0, "Blowin ' in the wind\n") >>> Line.insert (1, ' Bob dylan\n ') >>> line.append ( ' \n1962 by Warner bros.inc. ') >>> f.close () >>> f = open (R ' blowing in the Wind.txt ', ' W ') >>> f.writelines (line) >>> F.close ()
Add: List
with len () function to get the number of list elements;
Use an index to access the elements of each position in the list, remembering that the index is from the 0
start: list[0]
list append element to end: List.append ()
1
location: List.insert (1, Element)
Delete the element at the end of the list with pop ()
method: List.pop ( )
Delete the element at the specified position, using the pop (i)
method, where i
is the index location: List.pop (i)
Replacing an element with another element can be directly assigned to the corresponding index position: list[1] =
Integrated programming mini-project for "Local data acquisition"