Reputation: 197
I have a txt file that look like this
P SPB:0 curT:36C curNandT:25C
P SPB:1 curT:36C curNandT:25C
P SPB:2 curT:37C curNandT:28C
P SPB:3 curT:37C curNandT:29C
P SPB:4 curT:38C curNandT:29C
I want to separate them with ' ' and ':' how should I do it based on:
df = pd.read_csv(filename.txt, sep=???, header=None)
print(df)
Upvotes: 0
Views: 32
Reputation: 260335
You can use sep='[ :]'
pd.read_csv('filename', sep='[ :]', header=None)
output:
0 1 2 3 4 5 6
0 P SPB 0 curT 36C curNandT 25C
1 P SPB 1 curT 36C curNandT 25C
2 P SPB 2 curT 37C curNandT 28C
3 P SPB 3 curT 37C curNandT 29C
4 P SPB 4 curT 38C curNandT 29C
Upvotes: 3
Reputation: 9377
From Panda's docs on read_csv
, parameter sep
:
In addition, separators longer than 1 character and different from '\s+' will be interpreted as regular expressions and will also force the use of the Python parsing engine. Note that regex delimiters are prone to ignoring quoted data. Regex example: '\r\t'.
So you could pass separator argument as regex sep = "\s|:"
to split by whitespace or colon.
Upvotes: 0