Python Forum
Loop reading csv file problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loop reading csv file problem
#1
Read 1 csv file data code, it works well


import csv
import pandas as pd
import numpy as np
import torch

data = []
fname = 'C:/Users/faustineljc/Desktop/dia0_utt0.csv'  
with open(fname,'r+',encoding='utf-8') as f:
    for line in f.readlines():  
        mfcc = line.split(';')
        data.append(mfcc)
   
print(type(data))
data = np.array(data[0:100])
data = np.delete(data, 0, axis = 0)
data = np.delete(data, 0, axis = 1)
data = np.delete(data, 0, axis = 1)
data = np.array(data, dtype="float64").tolist()
#data type
print(type(data))
#list >>> tensor [sample, T, dimenstion]
mfcc = torch.tensor(data).unsqueeze(1).permute(1,0,2)
print(type(mfcc))
print(mfcc.shape)
print(mfcc)
however, An error occurs when operating on the data in the entire folder. It only read one file data.
the coding as follows:
import csv
import pandas as pd
import numpy as np
import os
import torch

csv_path_train = 'C:/Users/faustineljc/Desktop/d'
csv_files_train = os.listdir(csv_path_train)
csv_files_train.sort(key=lambda x: (int(x.split('_')[0][3:]), int(x.split('_')[1][3:-4])))
print(csv_files_train)

data = []
mfcc = []
for csv_file in csv_files_train:
    with open(csv_path_train + '/' + csv_file) as f:
        for line in f.readlines(): 
            mfcc = line.split(';')
            data.append(mfcc)
            
         
data = np.array(data[1:100])
data = np.delete(data, 0, axis=1)
data = np.delete(data, 0, axis=1)
data = np.array(data, dtype="float64").tolist()

mfcc = torch.tensor(data)
mfcc = mfcc.unsqueeze(1)
mfcc = mfcc.permute(1,0,2)

print(type(mfcc))
print(mfcc.shape)
print(mfcc)
Larz60+ write Dec-10-2021, 11:17 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use bbcode tags on future posts.
Reply
#2
You forgot to show us the error. Please show the complete error message and use the correct tags as Larz60+ wrote.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Sad problems with reading csv file. MassiJames 3 632 Nov-16-2023, 03:41 PM
Last Post: snippsat
  While Loop Problem Benno2805 1 580 Sep-06-2023, 04:51 PM
Last Post: deanhystad
  Reading a file name fron a folder on my desktop Fiona 4 916 Aug-23-2023, 11:11 AM
Last Post: Axel_Erfurt
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,102 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Reading a file JonWayn 3 1,095 Dec-30-2022, 10:18 AM
Last Post: ibreeden
  Reading Specific Rows In a CSV File finndude 3 989 Dec-13-2022, 03:19 PM
Last Post: finndude
  Excel file reading problem max70990 1 897 Dec-11-2022, 07:00 PM
Last Post: deanhystad
  Replace columns indexes reading a XSLX file Larry1888 2 989 Nov-18-2022, 10:16 PM
Last Post: Pedroski55
  Failing reading a file and cannot exit it... tester_V 8 1,818 Aug-19-2022, 10:27 PM
Last Post: tester_V
  Reading .csv file doug2019 4 1,703 Apr-29-2022, 09:55 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020