Create Pandas DataFrame from a string

后端 未结 5 1693
灰色年华
灰色年华 2020-11-22 09:01

In order to test some functionality I would like to create a DataFrame from a string. Let\'s say my test data looks like:

TESTDATA=\"\"\"col1;co         


        
5条回答
  •  眼角桃花
    2020-11-22 09:54

    Simplest way is to save it to temp file and then read it:

    import pandas as pd
    
    CSV_FILE_NAME = 'temp_file.csv'  # Consider creating temp file, look URL below
    with open(CSV_FILE_NAME, 'w') as outfile:
        outfile.write(TESTDATA)
    df = pd.read_csv(CSV_FILE_NAME, sep=';')
    

    Right way of creating temp file: How can I create a tmp file in Python?

提交回复
热议问题