How#

How to write to a file#

To write to a file use open with the w parameter:

Tip

with open(filename, "w") as temporary_name:
    ...

For example, the following creates a file with the values of integer \(n\) and \(n^2\) on each row for \(1\leq n \leq 100\).

with open("squares.csv", "w") as f:
    for n in range(1, 101):
        f.write(f"{n}, {n ** 2}\n")

How to append to a file#

To write to a file without deleting what is already in the file use open with the a parameter:

Tip

with open(filename, "a") as temporary_name:
    ...

For example, the following appends integer \(n\) and \(n^2\) on each row for \(101\leq n \leq 200\).

with open("squares.csv", "a") as f:
    for n in range(101, 201):
        f.write(f"{n}, {n ** 2}\n")

How to read from a file#

To read a file use open with the r parameter:

Tip

with open(filename, "r") as temporary_name:
    ...

For example, the following reads the data from squares.csv.

with open("squares.csv", "r") as f:
    data_as_string = f.read()

data_as_string
'1, 1\n2, 4\n3, 9\n4, 16\n5, 25\n6, 36\n7, 49\n8, 64\n9, 81\n10, 100\n11, 121\n12, 144\n13, 169\n14, 196\n15, 225\n16, 256\n17, 289\n18, 324\n19, 361\n20, 400\n21, 441\n22, 484\n23, 529\n24, 576\n25, 625\n26, 676\n27, 729\n28, 784\n29, 841\n30, 900\n31, 961\n32, 1024\n33, 1089\n34, 1156\n35, 1225\n36, 1296\n37, 1369\n38, 1444\n39, 1521\n40, 1600\n41, 1681\n42, 1764\n43, 1849\n44, 1936\n45, 2025\n46, 2116\n47, 2209\n48, 2304\n49, 2401\n50, 2500\n51, 2601\n52, 2704\n53, 2809\n54, 2916\n55, 3025\n56, 3136\n57, 3249\n58, 3364\n59, 3481\n60, 3600\n61, 3721\n62, 3844\n63, 3969\n64, 4096\n65, 4225\n66, 4356\n67, 4489\n68, 4624\n69, 4761\n70, 4900\n71, 5041\n72, 5184\n73, 5329\n74, 5476\n75, 5625\n76, 5776\n77, 5929\n78, 6084\n79, 6241\n80, 6400\n81, 6561\n82, 6724\n83, 6889\n84, 7056\n85, 7225\n86, 7396\n87, 7569\n88, 7744\n89, 7921\n90, 8100\n91, 8281\n92, 8464\n93, 8649\n94, 8836\n95, 9025\n96, 9216\n97, 9409\n98, 9604\n99, 9801\n100, 10000\n101, 10201\n102, 10404\n103, 10609\n104, 10816\n105, 11025\n106, 11236\n107, 11449\n108, 11664\n109, 11881\n110, 12100\n111, 12321\n112, 12544\n113, 12769\n114, 12996\n115, 13225\n116, 13456\n117, 13689\n118, 13924\n119, 14161\n120, 14400\n121, 14641\n122, 14884\n123, 15129\n124, 15376\n125, 15625\n126, 15876\n127, 16129\n128, 16384\n129, 16641\n130, 16900\n131, 17161\n132, 17424\n133, 17689\n134, 17956\n135, 18225\n136, 18496\n137, 18769\n138, 19044\n139, 19321\n140, 19600\n141, 19881\n142, 20164\n143, 20449\n144, 20736\n145, 21025\n146, 21316\n147, 21609\n148, 21904\n149, 22201\n150, 22500\n151, 22801\n152, 23104\n153, 23409\n154, 23716\n155, 24025\n156, 24336\n157, 24649\n158, 24964\n159, 25281\n160, 25600\n161, 25921\n162, 26244\n163, 26569\n164, 26896\n165, 27225\n166, 27556\n167, 27889\n168, 28224\n169, 28561\n170, 28900\n171, 29241\n172, 29584\n173, 29929\n174, 30276\n175, 30625\n176, 30976\n177, 31329\n178, 31684\n179, 32041\n180, 32400\n181, 32761\n182, 33124\n183, 33489\n184, 33856\n185, 34225\n186, 34596\n187, 34969\n188, 35344\n189, 35721\n190, 36100\n191, 36481\n192, 36864\n193, 37249\n194, 37636\n195, 38025\n196, 38416\n197, 38809\n198, 39204\n199, 39601\n200, 40000\n'

How to turn a string in to a list#

To split a string on a given character and turn it in to a list use the split method:

Tip

string.split()

For example to convert the data_as_string variable to a list of string:

data = data_as_string.split("\n")
data
['1, 1',
 '2, 4',
 '3, 9',
 '4, 16',
 '5, 25',
 '6, 36',
 '7, 49',
 '8, 64',
 '9, 81',
 '10, 100',
 '11, 121',
 '12, 144',
 '13, 169',
 '14, 196',
 '15, 225',
 '16, 256',
 '17, 289',
 '18, 324',
 '19, 361',
 '20, 400',
 '21, 441',
 '22, 484',
 '23, 529',
 '24, 576',
 '25, 625',
 '26, 676',
 '27, 729',
 '28, 784',
 '29, 841',
 '30, 900',
 '31, 961',
 '32, 1024',
 '33, 1089',
 '34, 1156',
 '35, 1225',
 '36, 1296',
 '37, 1369',
 '38, 1444',
 '39, 1521',
 '40, 1600',
 '41, 1681',
 '42, 1764',
 '43, 1849',
 '44, 1936',
 '45, 2025',
 '46, 2116',
 '47, 2209',
 '48, 2304',
 '49, 2401',
 '50, 2500',
 '51, 2601',
 '52, 2704',
 '53, 2809',
 '54, 2916',
 '55, 3025',
 '56, 3136',
 '57, 3249',
 '58, 3364',
 '59, 3481',
 '60, 3600',
 '61, 3721',
 '62, 3844',
 '63, 3969',
 '64, 4096',
 '65, 4225',
 '66, 4356',
 '67, 4489',
 '68, 4624',
 '69, 4761',
 '70, 4900',
 '71, 5041',
 '72, 5184',
 '73, 5329',
 '74, 5476',
 '75, 5625',
 '76, 5776',
 '77, 5929',
 '78, 6084',
 '79, 6241',
 '80, 6400',
 '81, 6561',
 '82, 6724',
 '83, 6889',
 '84, 7056',
 '85, 7225',
 '86, 7396',
 '87, 7569',
 '88, 7744',
 '89, 7921',
 '90, 8100',
 '91, 8281',
 '92, 8464',
 '93, 8649',
 '94, 8836',
 '95, 9025',
 '96, 9216',
 '97, 9409',
 '98, 9604',
 '99, 9801',
 '100, 10000',
 '101, 10201',
 '102, 10404',
 '103, 10609',
 '104, 10816',
 '105, 11025',
 '106, 11236',
 '107, 11449',
 '108, 11664',
 '109, 11881',
 '110, 12100',
 '111, 12321',
 '112, 12544',
 '113, 12769',
 '114, 12996',
 '115, 13225',
 '116, 13456',
 '117, 13689',
 '118, 13924',
 '119, 14161',
 '120, 14400',
 '121, 14641',
 '122, 14884',
 '123, 15129',
 '124, 15376',
 '125, 15625',
 '126, 15876',
 '127, 16129',
 '128, 16384',
 '129, 16641',
 '130, 16900',
 '131, 17161',
 '132, 17424',
 '133, 17689',
 '134, 17956',
 '135, 18225',
 '136, 18496',
 '137, 18769',
 '138, 19044',
 '139, 19321',
 '140, 19600',
 '141, 19881',
 '142, 20164',
 '143, 20449',
 '144, 20736',
 '145, 21025',
 '146, 21316',
 '147, 21609',
 '148, 21904',
 '149, 22201',
 '150, 22500',
 '151, 22801',
 '152, 23104',
 '153, 23409',
 '154, 23716',
 '155, 24025',
 '156, 24336',
 '157, 24649',
 '158, 24964',
 '159, 25281',
 '160, 25600',
 '161, 25921',
 '162, 26244',
 '163, 26569',
 '164, 26896',
 '165, 27225',
 '166, 27556',
 '167, 27889',
 '168, 28224',
 '169, 28561',
 '170, 28900',
 '171, 29241',
 '172, 29584',
 '173, 29929',
 '174, 30276',
 '175, 30625',
 '176, 30976',
 '177, 31329',
 '178, 31684',
 '179, 32041',
 '180, 32400',
 '181, 32761',
 '182, 33124',
 '183, 33489',
 '184, 33856',
 '185, 34225',
 '186, 34596',
 '187, 34969',
 '188, 35344',
 '189, 35721',
 '190, 36100',
 '191, 36481',
 '192, 36864',
 '193, 37249',
 '194, 37636',
 '195, 38025',
 '196, 38416',
 '197, 38809',
 '198, 39204',
 '199, 39601',
 '200, 40000',
 '']

Note that the last element is an empty row.

We can use the split method on each element of the data splitting on the , and also convert the entries to an integer.

data = [[int(n) for n in row.split(",")] for row in data_as_string.split("\n")[:-1]]
data
[[1, 1],
 [2, 4],
 [3, 9],
 [4, 16],
 [5, 25],
 [6, 36],
 [7, 49],
 [8, 64],
 [9, 81],
 [10, 100],
 [11, 121],
 [12, 144],
 [13, 169],
 [14, 196],
 [15, 225],
 [16, 256],
 [17, 289],
 [18, 324],
 [19, 361],
 [20, 400],
 [21, 441],
 [22, 484],
 [23, 529],
 [24, 576],
 [25, 625],
 [26, 676],
 [27, 729],
 [28, 784],
 [29, 841],
 [30, 900],
 [31, 961],
 [32, 1024],
 [33, 1089],
 [34, 1156],
 [35, 1225],
 [36, 1296],
 [37, 1369],
 [38, 1444],
 [39, 1521],
 [40, 1600],
 [41, 1681],
 [42, 1764],
 [43, 1849],
 [44, 1936],
 [45, 2025],
 [46, 2116],
 [47, 2209],
 [48, 2304],
 [49, 2401],
 [50, 2500],
 [51, 2601],
 [52, 2704],
 [53, 2809],
 [54, 2916],
 [55, 3025],
 [56, 3136],
 [57, 3249],
 [58, 3364],
 [59, 3481],
 [60, 3600],
 [61, 3721],
 [62, 3844],
 [63, 3969],
 [64, 4096],
 [65, 4225],
 [66, 4356],
 [67, 4489],
 [68, 4624],
 [69, 4761],
 [70, 4900],
 [71, 5041],
 [72, 5184],
 [73, 5329],
 [74, 5476],
 [75, 5625],
 [76, 5776],
 [77, 5929],
 [78, 6084],
 [79, 6241],
 [80, 6400],
 [81, 6561],
 [82, 6724],
 [83, 6889],
 [84, 7056],
 [85, 7225],
 [86, 7396],
 [87, 7569],
 [88, 7744],
 [89, 7921],
 [90, 8100],
 [91, 8281],
 [92, 8464],
 [93, 8649],
 [94, 8836],
 [95, 9025],
 [96, 9216],
 [97, 9409],
 [98, 9604],
 [99, 9801],
 [100, 10000],
 [101, 10201],
 [102, 10404],
 [103, 10609],
 [104, 10816],
 [105, 11025],
 [106, 11236],
 [107, 11449],
 [108, 11664],
 [109, 11881],
 [110, 12100],
 [111, 12321],
 [112, 12544],
 [113, 12769],
 [114, 12996],
 [115, 13225],
 [116, 13456],
 [117, 13689],
 [118, 13924],
 [119, 14161],
 [120, 14400],
 [121, 14641],
 [122, 14884],
 [123, 15129],
 [124, 15376],
 [125, 15625],
 [126, 15876],
 [127, 16129],
 [128, 16384],
 [129, 16641],
 [130, 16900],
 [131, 17161],
 [132, 17424],
 [133, 17689],
 [134, 17956],
 [135, 18225],
 [136, 18496],
 [137, 18769],
 [138, 19044],
 [139, 19321],
 [140, 19600],
 [141, 19881],
 [142, 20164],
 [143, 20449],
 [144, 20736],
 [145, 21025],
 [146, 21316],
 [147, 21609],
 [148, 21904],
 [149, 22201],
 [150, 22500],
 [151, 22801],
 [152, 23104],
 [153, 23409],
 [154, 23716],
 [155, 24025],
 [156, 24336],
 [157, 24649],
 [158, 24964],
 [159, 25281],
 [160, 25600],
 [161, 25921],
 [162, 26244],
 [163, 26569],
 [164, 26896],
 [165, 27225],
 [166, 27556],
 [167, 27889],
 [168, 28224],
 [169, 28561],
 [170, 28900],
 [171, 29241],
 [172, 29584],
 [173, 29929],
 [174, 30276],
 [175, 30625],
 [176, 30976],
 [177, 31329],
 [178, 31684],
 [179, 32041],
 [180, 32400],
 [181, 32761],
 [182, 33124],
 [183, 33489],
 [184, 33856],
 [185, 34225],
 [186, 34596],
 [187, 34969],
 [188, 35344],
 [189, 35721],
 [190, 36100],
 [191, 36481],
 [192, 36864],
 [193, 37249],
 [194, 37636],
 [195, 38025],
 [196, 38416],
 [197, 38809],
 [198, 39204],
 [199, 39601],
 [200, 40000]]