简体   繁体   中英

Splitting an array with \x00\t as the separator

I have imported from a csv file all the data by using:

import pandas as pd

# Import data using panda
df = pd.read_csv('ML_Cancelaciones_20190301.csv','rb', engine='python')
x = df.values

I used 'rb' as it was impossible using utf-8 and other methods. When I try utf-16 I get the following error:

ParserError: Expected 2 fields in line 6666, saw 3

I believe this might be due to a 'ñ' being present in this row.

Using 'rb' gives me an array with twice as many rows as the original csv file, with one row being empty and the other row containing all columns stack together. The answer looks like so:

array([['\x00'],
       [ 

    '\x001\x000\x004\x000\x001\x007\x00H\x001\x00\t\x006\x003\x00\t\x002\x000\x001\x006\x000\x008\x001\x002\x00\t\x009\x009\x009\x009\x009\x009\x009\x009\x00\t\x002\x000\x001\x006\x000\x008\x001\x002\x00\t\x001\x004\x00:\x005\x008\x00\t\x002\x000\x001\x006\x000\x008\x002\x006\x00\t\x002\x000\x001\x006\x000\x008\x002\x009\x00\t\x000\x00\t\x00D\x00e\x00s\x00c\x00o\x00n\x00o\x00c\x00i\x00d\x00o\x00\t\x000\x00\t\x00F\x00A\x00-\x00R\x00S\x00\t\x00F\x00a\x00c\x00t\x00u\x00r\x00a\x00d\x00a\x00 \x00(\x00N\x00)\x00\t\x00A\x00c\x00t\x00i\x00v\x00a\x00\t\x00E\x00s\x00t\x00a\x00d\x00o\x00s\x00 \x00U\x00n\x00i\x00d\x00o\x00s\x00\t\x002\x00\t\x00E\x00s\x00t\x00a\x00d\x00o\x00s\x00 \x00U\x00n\x00i\x00d\x00o\x00s\x00\t\x002\x00\t\x00E\x00.\x00E\x00.\x00U\x00.\x00U\x00.\x00\t\x00E\x00s\x00p\x00a\x00ñ\x00o\x00l\x00\t\x00E\x00S\x00\t\x00P\x00T\x00G\x00\t\x00P\x00r\x00e\x00s\x00t\x00i\x00g\x00e\x00\t\x00P\x00T\x00G\x00\t\x00P\x00r\x00e\x00s\x00t\x00i\x00g\x00e\x00\t\x00E\x00D\x00\t\x00E\x00-\x00D\x00i\x00s\x00t\x00r\x00i\x00b\x00u\x00t\x00i\x00o\x00n\x00\t\x00B\x002\x00B\x00\t\x00\t\x00D\x00e\x00s\x00c\x00o\x00n\x00o\x00c\x00i\x00d\x00o\x00\t\x00D\x00e\x00s\x00c\x00o\x00n\x00o\x00c\x00i\x00d\x00o\x00\t\x00P\x00r\x00o\x00m\x00o\x00c\x00i\x00o\x00n\x00a\x00l\x00\t\x00D\x00e\x00s\x00c\x00o\x00n\x00o\x00c\x00i\x00d\x00o\x00\t\x00T\x00o\x00d\x00o\x00 \x00I\x00n\x00c\x00l\x00u\x00i\x00d\x00o\x00\t\x001\x000\x000\x00%\x00\t\x00J\x00u\x00n\x00e\x00 \x00S\x00a\x00l\x00e\x00 \x00\t\x00J\x00u\x00n\x00e\x00 \x00S\x00a\x00l\x00e\x00 \x00\t\x00V\x00\t\x00N\x00u\x00e\x00v\x00a\x00\t\x00D\x00o\x00u\x00b\x00l\x00e\x00\t\x00N\x00o\x00\t\x00-\x009\x009\x00\t\x00\t\x00D\x00e\x00s\x00c\x00o\x00n\x00o\x00c\x00i\x00d\x00o\x00\t\x00\t\x00\t\x00D\x00e\x00s\x00c\x00o\x00n\x00o\x00c\x00i\x00d\x00o\x00\t\x00N\x00o\x00\t\x00N\x00o\x00\t\x00\t\x00\t\x00S\x00í\x00\t\x002\x00\t\x00P\x00e\x00n\x00d\x00i\x00e\x00n\x00t\x00e\x00 \x00d\x00e\x00 \x00C\x00o\x00b\x00r\x00o\x00\t\x00-\x009\x009\x00\t\x00D\x00e\x00s\x00c\x00o\x00n\x00o\x00c\x00i\x00d\x00o\x00\t\x00C\x00R\x00\t\x00C\x00r\x00é\x00d\x00i\x00t\x00o\x00\t\x003\x002\x001\x000\x009\x00\t\x001\x005\x000\x003\x001\x005\x00\t\x00D\x00e\x00s\x00c\x00o\x00n\x00o\x00c\x00i\x00d\x00o\x00\t\x00N\x00o\x00\t\x00E\x00.\x00E\x00.\x00U\x00.\x00U\x00.\x00\t\x00U\x00S\x00A\x00\t\x00P\x00A\x00B\x00L\x00O\x00\t\x00S\x00r\x00.\x00\t\x00\t\x00\t\x00D\x00e\x00s\x00c\x00o\x00n\x00o\x00c\x00i\x00d\x00o\x00\t\x00R\x00e\x00p\x00ú\x00b\x00l\x00i\x00c\x00a\x00 \x00D\x00o\x00m\x00i\x00n\x00i\x00c\x00a\x00n\x00a\x00\t\x003\x00\t\x006\x00\t\x002\x00\t\x002\x00\t\x000\x00\t\x002\x00\t\x004\x000\x008\x00,\x009\x006\x000\x000\x00'],
           ['\x00'],
           ..., 
           [ '\x00V\x003\x000\x004\x000\x001\x00H\x001\x00\t\x006\x001\x00\t\x002\x000\x001\x005\x000\x004\x001\x005\x00\t\x009\x009\x009\x009\x009\x009\x009\x009\x00\t\x002\x000\x001\x005\x000\x004\x001\x005\x00\t\x001\x006\x00:\x000\x000\x00\t\x002\x000\x001\x005\x000\x004\x001\x008\x00\t\x002\x000\x001\x005\x000\x004\x002\x006\x00\t\x000\x00\t\x00D\x00e\x00s\x00c\x00o\x00n\x00o\x00c\x00i\x00d\x00o\x00\t\x000\x00\t\x00F\x00A\x00-\x00R\x00S\x00\t\x00F\x00a\x00c\x00t\x00u\x00r\x00a\x00d\x00a\x00 \x00(\x00N\x00)\x00\t\x00A\x00c\x00t\x00i\x00v\x00a\x00\t\x00E\x00s\x00t\x00a\x00d\x00o\x00s\x00 \x00U\x00n\x00i\x00d\x00o\x00s\x00\t\x002\x00\t\x00E\x00s\x00t\x00a\x00d\x00o\x00s\x00 \x00U\x00n\x00i\x00d\x00o\x00s\x00\t\x002\x00\t\x00E\x00.\x00E\x00.\x00U\x00.\x00U\x00.\x00\t\x00E\x00n\x00g\x00l\x00i\x00s\x00h\x00\t\x00E\x00N\x00\t\x00B\x002\x00C\x00 \x00M\x00\t\x00W\x00e\x00b\x00 \x00C\x00a\x00l\x00l\x00 \x00C\x00e\x00n\x00t\x00e\x00r\x00\t\x00B\x002\x00C\x00\t\x00W\x00e\x00b\x00 \x00C\x00l\x00i\x00e\x00n\x00t\x00e\x00\t\x00B\x002\x00C\x00\t\x00B\x00u\x00s\x00i\x00n\x00e\x00s\x00s\x00-\x00t\x00o\x00-\x00C\x00u\x00s\x00t\x00o\x00m\x00e\x00r\x00\t\x00B\x002\x00C\x00\t\x00\t\x00D\x00e\x00s\x00c\x00o\x00n\x00o\x00c\x00i\x00d\x00o\x00\t\x00D\x00e\x00s\x00c\x00o\x00n\x00o\x00c\x00i\x00d\x00o\x00\t\x00P\x00r\x00o\x00m\x00o\x00c\x00i\x00o\x00n\x00a\x00l\x00\t\x00D\x00e\x00s\x00c\x00o\x00n\x00o\x00c\x00i\x00d\x00o\x00\t\x00T\x00o\x00d\x00o\x00 \x00I\x00n\x00c\x00l\x00u\x00i\x00d\x00o\x00\t\x001\x000\x000\x00%\x00\t\x00S\x00P\x00R\x00I\x00N\x00G\x00 \x00S\x00U\x00P\x00E\x00R\x00 \x00S\x00A\x00L\x00E\x00\t\x00S\x00P\x00R\x00I\x00N\x00G\x00 \x00S\x00U\x00P\x00E\x00R\x00 \x00S\x00A\x00L\x00E\x00\t\x00M\x00\t\x00M\x00o\x00d\x00i\x00f\x00i\x00c\x00a\x00d\x00a\x00\t\x00J\x00u\x00n\x00i\x00o\x00r\x00 \x00S\x00u\x00i\x00t\x00e\x00\t\x00N\x00o\x00\t\x00-\x009\x009\x00\t\x00\t\x00D\x00e\x00s\x00c\x00o\x00n\x00o\x00c\x00i\x00d\x00o\x00\t\x00\t\x00\t\x00D\x00e\x00s\x00c\x00o\x00n\x00o\x00c\x00i\x00d\x00o\x00\t\x00N\x00o\x00\t\x00N\x00o\x00\t\x00\t\x00\t\x00N\x00o\x00\t\x002\x00\t\x00P\x00e\x00n\x00d\x00i\x00e\x00n\x00t\x00e\x00 \x00d\x00e\x00 \x00C\x00o\x00b\x00r\x00o\x00\t\x00-\x009\x009\x00\t\x00D\x00e\x00s\x00c\x00o\x00n\x00o\x00c\x00i\x00d\x00o\x00\t\x00T\x00C\x00\t\x00T\x00a\x00r\x00j\x00e\x00t\x00a\x00 \x00C\x00r\x00é\x00d\x00i\x00t\x00o\x00\t\x00-\x009\x009\x00\t\x00-\x009\x008\x00\t\x00D\x00e\x00s\x00c\x00o\x00n\x00o\x00c\x00i\x00d\x00o\x00\t\x00N\x00o\x00\t\x00D\x00e\x00s\x00c\x00o\x00n\x00o\x00c\x00i\x00d\x00o\x00\t\x00-\x009\x009\x00\t\x00D\x00I\x00L\x00L\x00O\x00N\x00\t\x00\t\x001\x009\x006\x001\x00-\x001\x002\x00-\x001\x007\x00 \x000\x000\x00:\x000\x000\x00:\x000\x000\x00\t\x00\t\x00E\x00.\x00E\x00.\x00U\x00.\x00U\x00.\x00\t\x00E\x00s\x00t\x00a\x00d\x00o\x00s\x00 \x00U\x00n\x00i\x00d\x00o\x00s\x00\t\x008\x00\t\x003\x002\x00\t\x004\x00\t\x003\x00\t\x001\x00\t\x000\x00\t\x003\x004\x001\x006\x00,\x000\x000\x000\x000\x00'],
           ['\x00'],
           ['\x00']], dtype=object)

I wanted to convert this array so that it has a shape nrowsxncolumns of the original csv file. I also wanted the entries to be the same as the original file, ie words and numbers.

How could I do this?

An example of the data is here: csv file opened with word pad

The row: '\\x001\\x000\\x004\\x000\\x001\\x007\\x00H\\x001\\x00\\t\\x006\\x003\\x00\\t\\x002\\x000\\x001\\x006\\x000\\x008\\x001\\x002\\x00\\t\\...' should look like:

['104017H1', '63', '20160812',...]

Therefore all values have a '\\x00' before them and each column is separated by a '\\x00\\t. Is there a way I can do this trasformation?

Thank you very much

You can try replace and split :

a = '\x001\x000\x004\x000\x001\x007\x00H\x001\x00\t\x006\x003\x00\t\x002\x000\x001\x006\x000\x008\x001\x002\x00\t'
a.replace('\x00','').split('\t')

OUTPUT :

['104017H1', '63', '20160812', '']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM