Академический Документы
Профессиональный Документы
Культура Документы
Try:
Advanced data type - List list. The length of the list increases by number of elements in it’s argument.
Indexing
List is a versatile data type. It can contain multiple values with different data types.
For each item in a list, its position is represented by its index.
*What is a "method"?
Python has similar indexing rules with C++, where the indices starts from 0. The last element of
Method is a function defined under a class or an object. a list is index (len - 1)
A list is an object in python and we can apply some "method" to it. In Python, indices can also be used as negative numbers, which means the placement from the
end of a list. The last element of a list can also be located with index -1, the second-to-last can
How to use a method?
be located -2 and so on.
1. Create an instance of that object
Note: Be careful with the 'index out of range' error. For a list of length LEN, the available
2. format: list.method() indexing domain is [-LEN, LEN - 1].
Examples Example
Useful methods for list print(names[4]) # IndexError: list index out of range
print(names[-5])
# start:end
I/O files
print(p[2:4]) # [2, 3]
print(p[:4]) # [0, 1, 2, 3]
Built-in open() function:
print(p[2:]) # [2, 3, 4, 5]
print(p[2:-2]) # [2, 3] To open a file for writing, use the built-in open() function. open() returns a file object, and is
most commonly used with two arguments.
# start:end:step
print(p[1:5:2]) # [1, 3]
file_object = open(filename, mode)
print(p[0::2]) # [0, 2, 4]
print(p[1::2]) # [1, 3, 5]
The mode can be:
print(p[4:0:-1]) # [4, 3, 2, 1]
'r' when the file will only be read
print(p[::-1]) # [5, 4, 3, 2, 1, 0]
'pointer' stays at the beginning of the file.
'w' for only writing (an existing file with the same name will be erased)
If you want to avoid the problem of list index out of range, you can utilize len() .
File existing: cover the file with your input
lst = ['ser','wer','wt4','rw34','ewr','ewr3','dse','t4','se'] File not existing: creat a new file
for i in range(0,len(lst)): #range(0,n)=[0,1,2, ... , n-1] 'a' opens the file for appending; any data written to the file is automatically added to
print(lst[i]) the end
Notes:
You can refresh the position of the pointer to the beginning with file.seek(0) .
# in Mac/Linux:
Example: open('/etc/file/onefile')
# or in Windows:
file = open('Poem.txt','r') open('C:\Users\user\Desktop\onefile.txt')
print(file.read()) You can use terminal to find the absolute path of a file.
file.seek(0)
print('------------')
If your file is in the same folder with your code file, you can use relative paths.
print(file.read(10))
print('------------') Read CSV or EXCEL file
print(file.readline())
Here you need to import a module called pandas .
print(file.readline())
print('-------------')
print(file.readlines()) import pandas as pd
print('-------------') file1 = pd.read_csv('spreadsheet.csv')
file.seek(0) file2 = pd.read_excel('spreadsheets.xlsx')
for i in file.readlines(): # read by line and put each line into a list
print(i)
Append a file