Error while working on .csv with load_csv

I am trying to work on the below code:

ds = load_csv('C:\\User.csv')
f = open(ds,'r')
lines = f.readlines()[1:]
print(lines)
f.close()

First line of dataset is string. I am getting the below error:

TypeError: expected str, bytes or os.PathLike object, not list

Though when I try to open the file with below code it works:

filename='C:\\User.csv'
f = open(filename,'r')
lines = f.readlines()[1:]
print(lines)
f.close()

I am ignoring the first line because its string and rest of the dataset is float.

Update:

load_csv

def load_csv(ds):
    dataset = list()
    with open(ds, 'r') as file:
        csv_reader = reader(file)
        for row in csv_reader:
            if not row:
                continue
            dataset.append(row)
            return dataset

Even if I use this way still get the error:

ds = load_csv('C:\\Users.csv')
minmax = dataset_minmax(ds)
normalize_dataset(ds, minmax)

def dataset_minmax(dataset):
    minmax = list()
    for i in range(len(dataset[0])):
        col_values = [row[i] for row in dataset]
        value_min = min(col_values)
        value_max = max(col_values)
        minmax.append([value_min, value_max])
    return minmax

def normalize_dataset(dataset, minmax):
    for row in dataset:
        for i in range(len(row)):
            row[i] = (row[i] - minmax[i][0]) / (minmax[i][1] - minmax[i][0])

It gives error on:

row[i] = (row[i] - minmax[i][0]) / (minmax[i][1] - minmax[i][0])

Error:

TypeError: unsupported operand type(s) for -: 'str' and 'str'

Since you're now getting a different error, I'll give a second answer.

This error means that the two variables in your subtraction are strings, not numbers.

In [1]: 5 - 3
Out[1]: 2

In [2]: '5' - '3'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-4ef7506473f1> in <module>
----> 1 '5' - '3'

TypeError: unsupported operand type(s) for -: 'str' and 'str'

This is because the CSV reader assumes everything is a string. You need to convert it to floats, e.g., by changing load_csv to do something like dataset.append(list(map(float, row))) instead of your existing append statement.

The min-max stuff doesn't fail, because Python's min and max work on strings, too:

In [3]: min('f', 'o', 'o', 'b', 'a', 'r')
Out[3]: 'a'

However, it might be giving you incorrect answers:

In [4]: min('2.0', '10.0')
Out[4]: '10.0'

By the way, if you're doing much along these lines, you'd probably benefit from using the Pandas package instead of rolling your own.

Error while reading a csv file in pandas - tools, the csv file import pandas as pd df=pd.read_csv(“data.csv”) the error i am the extension to the .csv after when user use it then it won't work. The csv module is iterating over the file object. It relies on the fact, that a iterating over a file, which has been opened in text mode, yields one line per iteration step.

I am guessing the error is in the open command in your code. The reason why this fails is that the open command expects a string or operating system path-like object that is a handle to a file that it can open (like it says in the error). The function load_csv probably returns a list which is an incompatible format for open

Error - unable to read the csv file in pandas - tools, What directory are you working in? CSV data formats and errors – common errors with the function. Each of these topics is discussed below, and we finish this� CSV Kit is the best utility that I’ve found for working with CSV files. It’s a free set of tools for dealing with CSV files on Linux. Some of the cool things it can do are: CSV clean will validate and clean the file of common syntax errors. It isn’t magic, but can definitely help. CSV grep is incredibly useful.

Look at your first two lines where it doesn't work:

ds = load_csv('C:\\User.csv')
f = open(ds,'r')

ds is an object returned (from TensorFlow, I assume?) which contains the data. Then you open it as if it were a filename. This is why the interpreter complains. ds is the dataset, not the string representing the file.

It works in the other example, because you use a filename.

Python Pandas read_csv: Load Data from CSV Files, 'c,d,3') .: In [32]: pd.read_csv(StringIO(data)) Out[32]: col1 col2 col3 0 a b 1 1 a b 2 2 c d 3 In [33]: Lines with too many fields will raise an error by default: In [144]: Generally the semantics are similar to working with csv data. Recall that CSV files are just plain text files with values delimited by commas (or sometimes tabs). Piping Import-Csv to a Format-* PowerShell cmdlet will not work as expected. Per Microsoft Export-Csv documentation: “Do not format objects before sending them to the Export-CSV cmdlet.

IO tools (text, CSV, HDF5, …) — pandas 1.1.0 documentation, Read a comma-separated values (csv) file into DataFrame. Also supports optionally iterating or breaking of the file into chunks. Additional help can be found in the� I tried to upload my CSV and a Choose column headings window appeared. This window appears when the headers of your CSV file are incorrect. You can choose the correct headers from the drop-down menu to help you import your CSV file to Shopify.

pandas.read_csv — pandas 1.1.0 documentation, Pandas Tutorial: Importing Data with read_csv() Often, you'll work with data in Comma Separated Value (CSV) files and run into problems at� Solution. If you have a test.csv file (sample .csv file attached to this article) that has 7 columns and you are interested in loading only some columns out of the entire list of columns in the test.csv file, you can use several ways to achieve this objective.

Importing Data with Pandas' read_csv(), Quite frequently, the sample data is in Excel format, and needs to be imported into R The sample data can also be in comma separated values (CSV) format. Finally, the code samples above assume the data files are located in the R working Type II Error in Lower Tail Test of Population Mean with Known Variance� For error-free viewing and saving of CSV files, we recommend using an application built for CSV compatibility, such as CSVEdit, Sublime Text, Apache OpenOffice, TextEdit, or Notepad. Trailing Commas. Saving an Excel file as a CSV file can create extra commas at the end of each row.

Comments
  • Where is the load_csv function from? It’s common to use pandas read_csv function.
  • Also in future, add line numbers and mention the line where you get the error!
  • updated with load_csv
  • even if i change that line to dataset.append(list(map(float, row))), it still giving error 'dataset.append(list(map(float, row))) ValueError: could not convert string to float: '7;0.27;0.36;20.7;0.045;45;170;1.001;3;0.45;8.8;6''
  • Your input data is semi-colon separated, not comma separated. CSV would have a line '7,0.27,0.36,...' instead of '7;0.27;0.36;...'. Use delimiter=';' when you create the CSV reader.
  • row[i] = (row[i] - minmax[i][0]) / (minmax[i][1] - minmax[i][0]) ZeroDivisionError: float division by zero
  • That's a different error. You have a row where the min and max values are the same, apparently. You might want to try printing out the intermediate data and seeing what you have. Or take advantage of other tools that exist: load it in pandas and then do this: stackoverflow.com/a/41532180/4205735
  • After your update it seems that my explanation were made with reasonable assumptions :)
  • you can delete this answer so we can work on above one