with open(file, 'w') as f: can write csv files directly (though file is CLAIMED to be String only)

1
2
3
4
5
6
7
8
9
# better: dataframe -> csv; then read: csv -> dataframe
def write_expected_csv(self, query_func, data):
print(os.getcwd())
root_path = './tests/data/chinook/expected_data'
file = os.path.join(root_path, query_func.__name__ + '.csv')
# with open(file, 'w') as f:
# f.write(data.to_string())
with open(file, 'w') as f:
f.write(data.to_csv())
  • with open writes csv:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    ,full_name
    0,Aaron Mitchell
    1,Alexandre Rocha
    2,Astrid Gruber
    3,Bjørn Hansen
    4,Camille Bernard
    5,Daan Peeters
    6,Dan Miller
    7,Diego Gutiérrez
    8,Dominique Lefebvre
    9,Eduardo Martins
  • with open writes string:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
            full_name
    0 Aaron Mitchell
    1 Alexandre Rocha
    2 Astrid Gruber
    3 Bjørn Hansen
    4 Camille Bernard
    5 Daan Peeters
    6 Dan Miller
    7 Diego Gutiérrez
    8 Dominique Lefebvre
    9 Eduardo Martins

When reading to dataframe, it would be quite easy to deal with csv files while this is not the case with string files. Note that the files would be in extension .csv.

In that case, why bother using io.StringIO() to transform dataframe into a string then csv? Reverting back would be arduous.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def read_expected_csv(self, query_func):
root_path = './tests/data/chinook/expected_data'
file = os.path.join(root_path, query_func.__name__ + '.csv')
print()
# with open(file, 'r') as f:
# res = f.read()
# print("res 11111")
# print(type(res))
# print(res)
#
import pandas as pd
# no need to read via a tmp = io.StringIO() as a bridge
# from df to csv, with.open() can directly read csv into
# csv file
# from io import StringIO
# res = pd.read_csv(StringIO(res), index_col=False)
# print("res 22222")
# print(type(res))
# print(res)
res = pd.read_csv(file, index_col=0)
# print("res 22222")
# print(type(res))
# print(res)
return res