f = open('workfile', 'w') f.read() f.readline() f.write('This is a test\n') for line in f: print(line, end='') value = ('the answer', 42) s = str(value) f = open('workfile', 'rb+') f.write(b'0123456789abcdef') f.seek(5) f.read(1) f.seek(-3, 2) f.read(1) f.close() f.read() with open('workfile', 'r') as f: read_data = f.read() f.closed data.txt 3 4 0.0 0.12 0.4 0.70 1.02 0.00 0.01 0.02 2.0 1.12 1.3 1.7 ÀÌ·± µ¥ÀÌÅ͸¦ A(4,3)Â¥¸® ¹è¿­¿¡ ³Ö°í ½Í½À´Ï´Ù. array('l') array('u', 'hello \u2641') array('l', [1, 2, 3, 4, 5]) array('d', [1.0, 2.0, 3.14]) import array s = array.array('0.0', '0.12', '0.4', '0.70') str(s) s = bytearray.split('0.0', '0.12', '0.4', '0.70') str(s) 1.02 0.00 0.01 0.02 2.0 1.12 1.3 1.7 //-------------------------------------------- //https://docs.python.org/3/library/array.html //'f' float float 4 //-------------------------------------------- //https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex //https://docs.python.org/3/library/stdtypes.html#binary-sequence-types-bytes-bytearray-memoryview a = array.array('b', ['0.0', '0.12', '0.4', '0.70', '1.02', '0.00', '0.01', '0.02', '2.0', '1.12', '1.3', '1.7']) / / / import array import math a = array.array('f', [0.0, 0.12, 0.4, 0.70, 1.02, 0.00, 0.01, 0.02, 2.0, 1.12, 1.3, 1.7]) m = memoryview(a) len(m) print a print m float(a[1:3]) float(m[1:3]) print ('%(val)f' %{'val':float(m[1:3])}) aa = array.array('b', ['0.0', '0.12', '0.4', '0.70', '1.02', '0.00', '0.01', '0.02', '2.0', '1.12', '1.3', '1.7']) mm = memoryview(aa) aa.tobytes() aa.tostring() str(mm[1:3]) #http://www.thegeekstuff.com/2013/08/python-array/ #//---------- #//flat Çü Ãâ·ÂÇϱ⠼º°ø #//---------- from array import * from array import * my_array = array('f', [1.1,2.2,3.3,4.4,5.5]) for f in my_array: print(f) my_array2 = array('f', [1.1,2.2,3.3,4.4,5.5]) for i in my_array2: print(i) #//---------- #//float Çü ºÐ¸®Çϱ⠼º°ø #//---------- ar_float = array('f', [0.0, 0.12, 0.4, 0.70, 1.02, 0.00, 0.01, 0.02, 2.0, 1.12, 1.3, 1.7]) print(ar_float[0],ar_float[1],ar_float[2]) print(ar_float[3],ar_float[4],ar_float[5]) print(ar_float[6],ar_float[7],ar_float[8]) print(ar_float[9],ar_float[10],ar_float[11]) #//---------- #//¹®ÀÚ¿­ ºÐ¸®Çϱ⠼º°ø #//---------- byte = "0.0 0.12 0.4 0.70 1.02 0.00 0.01 0.02 2.0 1.12 1.3 1.7" ar = byte.split(" ") for i in ar: print(i) #//---------- #//ÆÄÀÏ ÀúÀåÇϱâ #//---------- f = open('data.txt', 'w') f.write("0.0 0.12 0.4 0.70\n") f.write('1.02 0.00 0.01 0.02\n') f.write('2.0 1.12 1.3 1.7\n') f.close() #//https://docs.python.org/3/tutorial/introduction.html#strings #//https://docs.python.org/2/tutorial/introduction.html #//https://docs.python.org/2.3/whatsnew/section-slices.html #//---------- #//ÆÄÀÏ Àбâ #//---------- f = open('data.txt', 'rt') #print (f.readline()) #print (f.read()) byte1 = f.readline() ar1 = byte1.split(" ") byte2 = f.readline() ar2 = byte2.split(" ") byte3 = f.readline() ar3 = byte3.split(" ") f.close() tmp = ar1[len(ar1)-1].split("\n") ar1[len(ar1)-1] = tmp[0]; tmp = ar2[len(ar2)-1].split("\n") ar2[len(ar2)-1] = tmp[0]; tmp = ar3[len(ar3)-1].split("\n") ar3[len(ar3)-1] = tmp[0]; print (ar1) print (ar2) print (ar3) print (ar1[0],ar2[0],ar3[0]) print (ar1[1],ar2[0],ar3[0]) print (ar1[2],ar2[0],ar3[0]) print (ar1[3],ar2[0],ar3[0]) //¿À·ù ar_byte = array('b', ['0.0', '0.12', '0.4', '0.70', '1.02', '0.00', '0.01', '0.02', '2.0', '1.12', '1.3', '1.7']) mv = memoryview(ar_byte) str(ar_byte[1:4]) //http://www.dotnetperls.com/bytes elements = [0, 200, 50, 25, 10, 255] # Create bytearray from list of integers. values = bytearray(elements) # Modify elements in the bytearray. values[0] = 5 values[1] = 0 # Display bytes. for value in values: print(value) // elements = ['0.0', '0.12', '0.4', '0.70', '1.02', '0.00', '0.01', '0.02', '2.0', '1.12', '1.3', '1.7'] elements = ['0.0, 0.12, 0.4, 0.70, 1.02, 0.00, 0.01, 0.02, 2.0, 1.12, 1.3, 1.7'] elements = ['0.0', '0.12', '0.4', '0.70', '1.02', '0.00', '0.01', '0.02', '2.0', '1.12', '1.3', '1.7'] data = bytes(elements) first_part = data[0:2] # Display values from slice. for element in first_part: print(element) // elements = ['0.0, 0.12, 0.4, 0.70, 1.02, 0.00, 0.01, 0.02, 2.0, 1.12, 1.3, 1.7'] mv = memoryview(elements) str(mv[1:4]) // elements = "0.0, 0.12, 0.4, 0.70, 1.02, 0.00, 0.01, 0.02, 2.0, 1.12, 1.3, 1.7" result = bytes( elements.decode("ascii") ) print(result) Array creation http://docs.scipy.org/doc/numpy/user/basics.creation.html a.split(b',') b'1.1,2.1,3.1'.split(b',') TypeError: an integer is required (got type str) print('%(language)s has %(number)03d quote types.' %{'language': "Python", "number": 2}) Python has 002 quote types. bytes(m[1:3]) bytes(m[4:6]) bytes(m[7:9]) bytes(m[10:12]) float.hex(m[1:3]) float(m[4:6]) float(m[7:9]) float(m[10:12]) str(m[1:3]) str(m[4:6]) str(m[7:9]) str(m[10:12]) float.from_bytes(f'10.1', byteorder='big') TypeError: a float is required http://stackoverflow.com/questions/15003403/typeerror-a-float-is-required // >>> v = memoryview(b'abcefg') >>> v[1] 98 >>> v[-1] 103 >>> v[1:4] >>> bytes(v[1:4]) b'bce' %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object', use 'object??' for extra details. # Python 3: Fibonacci series up to n >>> def fib(n): >>> a, b = 0, 1 >>> while a < n: >>> print(a, end=' ') >>> a, b = b, a+b >>> print() >>> fib(1000) lines = file1.readlines() for line in lines: file2.write(line) dum1, dum2, dum3, dum4 = line.split() lists = [[]] * 3 lists [[], [], []] lists[0].append(3) lists [[3], [3], [3]] What has happened is that [[]] is a one-element list containing an empty list, so all three elements of [[]] * 3 are references to this single empty list. Modifying any of the elements of lists modifies this single list. You can create a list of different lists this way: >>> lists = [[] for i in range(3)] lists[0].append(3) lists[1].append(5) lists[2].append(7) lists [[3], [5], [7]] list(range(10)) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] list(range(1, 11)) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] list(range(0, 30, 5)) [0, 5, 10, 15, 20, 25] list(range(0, 10, 3)) [0, 3, 6, 9] list(range(0, -10, -1)) [0, -1, -2, -3, -4, -5, -6, -7, -8, -9] list(range(0)) [] list(range(1, 0)) [] int.from_bytes(b'\x00\x10', byteorder='big') 16 int.from_bytes(b'\x00\x10', byteorder='little') 4096 int.from_bytes(b'\xfc\x00', byteorder='big', signed=True) -1024 int.from_bytes(b'\xfc\x00', byteorder='big', signed=False) 64512 int.from_bytes([255, 0, 0], byteorder='big') 16711680 '1,2,3'.split(',') ['1', '2', '3'] '1,2,3'.split(',', maxsplit=1) ['1', '2,3'] '1,2,,3,'.split(',') ['1', '2', '', '3', ''] The Python Standard Library https://docs.python.org/3/library/index.html 8.7. array ? Efficient arrays of numeric values https://docs.python.org/3/library/array.html 4. Built-in Types https://docs.python.org/3/library/stdtypes.html#binary-sequence-types-bytes-bytearray-memoryview The Python Tutorial https://docs.python.org/3/tutorial/index.html 7. Input and Output https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files Python/C API Reference Manual https://docs.python.org/3/c-api/index.html // ¼¼°è°úÇбâ¼ú¹ßÀüÀÇ ÀÌÁ¤Ç¥ ¡®´ëÀü¼±¾ð¹®¡¯ÀÇ ³»¿ëÀº? http://www.newscj.com/news/articleView.html?idxno=313944