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
  Problem with For loop indentation Mormolyce 10 2,980 Sep-03-2025, 03:55 AM
Last Post: DeaD_EyE
  Reading an ASCII text file and parsing data... oradba4u 2 2,798 Jun-08-2024, 12:41 AM
Last Post: oradba4u
  problem program runs in a loop jasserin 0 1,317 May-18-2024, 03:07 PM
Last Post: jasserin
Sad problems with reading csv file. MassiJames 3 3,946 Nov-16-2023, 03:41 PM
Last Post: snippsat
  While Loop Problem Benno2805 1 1,640 Sep-06-2023, 04:51 PM
Last Post: deanhystad
  Reading a file name fron a folder on my desktop Fiona 4 3,238 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 3,038 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Reading a file JonWayn 3 2,655 Dec-30-2022, 10:18 AM
Last Post: ibreeden
  Reading Specific Rows In a CSV File finndude 3 2,587 Dec-13-2022, 03:19 PM
Last Post: finndude
  Excel file reading problem max70990 1 2,285 Dec-11-2022, 07:00 PM
Last Post: deanhystad

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.