0

I am trying to split data with multiple delimiters from a txt file. I've tried all sorts but for some reason this will not work

The data looks like this.

MOD001|Zacharias Karstensen|2|MOD002,MOD003,MOD004,MOD005,MOD006,MOD007,MOD008,MOD009,MOD010,MOD013
        
MOD002|Dominykas Cleary|2|MOD001,MOD003,MOD004,MOD005,MOD006,MOD007,MOD008,MOD009,MOD010,MOD013
        
MOD003|Zacharias Karstensen|2|MOD001,MOD002,MOD004,MOD005,MOD006,MOD007,MOD008,MOD009,MOD010,MOD011,MOD012,MOD013

I am trying to split by pipe (|) and commas (,), then store the results in a 2d array.

I'm reading the data in the following manner:

with open('Modules.txt', 'r') as fr:
    data = fr.read()
    
data = data.split('\n')

I can get it to work with a pipe delimiter like so:

data = [n.split('|') for n in data]
data = pd.DataFrame(data)
modules = np.array(data)

Your help is greatly appreciated.

Thanks!

Sam
  • 1
  • 1
  • What else have you tried here... have you actually tried splitting by | or commas at some point? If you have - you might already have some code that someone can help you correct/work with – Jon Clements Apr 26 '22 at 13:29
  • You haven't written any code to split anything by pipe or comma. Please make an attempt and then asks specific question if you run into an issue – Pranav Hosangadi Apr 26 '22 at 13:29
  • Use the `csv` module, and specify a delimiter of `|`. You'll have to split the comma-separated field yourself after the fact. (I don't *think* `csv` can handle multiple delimiters, but I could be mistaken.) – chepner Apr 26 '22 at 13:29
  • Sorry, I forgot to put my code in, I have done so now – Sam Apr 26 '22 at 13:35
  • https://stackoverflow.com/questions/41847146/multiple-delimiters-in-single-csv-file – Amiga500 Apr 26 '22 at 13:35
  • https://stackoverflow.com/questions/26551662/import-text-to-pandas-with-multiple-delimiters – Pranav Hosangadi Apr 26 '22 at 13:35

0 Answers0