pandas read_csv multiple separator 1

pandas read_csv multiple separator

my_cols = [str(i) for i in range(45)] # create some col names
df_user_key_word_org = pd.read_csv(filepath+"user_key_word.txt",
                                   sep="\s+|;|:",
                                   names=my_cols, 
                                   header=None, 
                                   engine="python")
# I tested with s = StringIO(text_from_OP) on my computer

Here is what the above code is Doing:
1. The first line is creating a list of strings from 0 to 44.
2. The second line is reading in the file, using the list of strings as column names.
3. The third line is creating a StringIO object from the text you posted.

The reason you need to use StringIO is because the text you posted is not a file. It’s just a string.

The reason you need to use the list of strings as column names is because the text you posted does not have a header.

The reason you need to use the engine=”python” is because the text you posted has a mix of whitespace and non-whitespace as delimiters.

The reason you need to use the sep=”\s+|;|:” is because the text you posted has a mix of whitespace and non-whitespace as delimiters.

Similar Posts