Python module csv question!!! I'd like to ask you a data processing question about the values in the list

Asked 2 years ago, Updated 2 years ago, 75 views

I am using Python module csv to work on the csv file

Among the values I loaded the csv file,

For example, list = ['0', '0', '0'] There's a lot of data in the list.

There's a value called ' in the third one, and I think we need to remove it so that we can change it to float type and calculate it.

I don't know how to remove that Unable to remove with newline=',

if not' in list: Even if you squeeze it like this, it's still there if you print it out.

The module used csv and how can I remove these empty values from the list?

The code is complicated, so you don't have to refer to it.

I want to release it using CSV except for using the number-fi module!!

from tkinter import filedialog

import os

import csv

import numpy as np

from tqdm import *

Root = filedialog.askdirectory()

FileList = os.listdir(Root)

RefHeight = [Folder for Folder in FileList if Folder.endswith('csv')]

one_dimensional = []

lowerlist = []


for k in range(len(RefHeight)):

    f = open(os.path.join(Root,RefHeight[k]),'r',newline='')
    data = csv.reader(f,skipinitialspace=True) # Remove spaces before data

    for row in data:
        one_dimensional.extend(row) # Makes it one dimensional

        # # one_dimensional = list(map(float, one_dimensional))
        # # sorted(one_dimensional, reverse=True)
            # # for i in tqdm(range(len(one_dimensional))):
print(one_dimensional)

csv list

2022-09-21 17:41

1 Answers

If you don't mind using the for statement

result = []

for data in list:
    new_data = data.strip()
    if new_data == '':
        continue

    new_data = data
    result.append(new_data)

print(result)

This will allow you to create a list of all but spaces and move on to the next step.


2022-09-21 17:41

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.