Handle variable as file with pandas dataframe

I would like to create a pandas dataframe out of a list variable. With pd.DataFrame() I am not able to declare delimiter which leads to just one column per list entry.

If I use pd.read_csv() instead, I of course receive the following error

ValueError: Invalid file path or buffer object type: <class ‘list’>

If there a way to use pd.read_csv() with my list and not first save the list to a csv and read the csv file in a second step?

I also tried pd.read_table() which also need a file or buffer object.

Example data (seperated by tab stops):

Col1    Col2    Col3
12      Info1   34.1
15      Info4   674.1

test = [“Col1tCol2tCol3”, “12tInfo1t34.1″,”15tInfo4t674.1”]

Current workaround:

with open(f'{filepath}tmp.csv', 'w', encoding='UTF8') as f:
    [f.write(line + "n") for line in consolidated_file]

df = pd.read_csv(f'{filepath}tmp.csv', sep='t', index_col=1 ) 


It seems simpler to convert it to nested list like in other answer

import pandas as pd

test = ["Col1tCol2tCol3", "12tInfo1t34.1","15tInfo4t674.1"]

data = [line.split('t') for line in test]

df = pd.DataFrame(data[1:], columns=data[0])

but you can also convert it back to single string (or get it directly form file on socket/network as single string) and then you can use io.BytesIO or io.StringIO to simulate file in memory.

import pandas as pd
import io

test = ["Col1tCol2tCol3", "12tInfo1t34.1","15tInfo4t674.1"]

single_string = "n".join(test)

file_like_object = io.StringIO(single_string)

df = pd.read_csv(file_like_object, sep='t')

or shorter

df = pd.read_csv(io.StringIO("n".join(test)), sep='t')

This method is popular when you get data from network (socket, web API) as single string or data.