i've a txt file that contains something like this:
Struct{a,b,c}
Struct {d,f,z}
I'd like to read this file and have this output:
0 1 2
0 a b c
1 d f z
I tried to run this code
import numpy as np
import pandas as pd
def conv(val, default_val=''):
try:
return (val)
except ValueError:
return default_val
x = pd.read_csv('/Users/me/Desktop/connector/connector.txt', sep=",", header=None, converters={'Struct': conv, '{': conv, '}': conv })
print(x)
but this is the output:
0 1 2
0 Struct{x y z}
1 Struct{a b c}
can somebody tell me why it doesn't work?
This isn't working because parameter converters
expects a dict of functions for converting values in certain columns. Keys can either be integers or column labels. None of ['Struct','{','}']
is a column label.
In order to read this text file, you would need to preprocess it getting rid of 'Struct', '{', '}'
. I.e. you will need to convert it to a proper CSV file.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments