How to Read a File with a Semi Colon Separator in Pandas

How to read a file with a semi colon separator in pandas

read_csv takes a sep param, in your case just pass sep=';' like so:

data = read_csv(csv_path, sep=';')

The reason it failed in your case is that the default value is ',' so it scrunched up all the columns as a single column entry.

Python - How can I check if a CSV file has a comma or a semicolon as a separator?

Say that you would like to read an arbitrary CSV, named input.csv, and you do not know whether the separator is a comma or a semicolon.

You could open your file using the csv module. The Sniffer class is then used to deduce its format, like in the following code:

import csv
with open(input.csv, newline='') as csvfile:
dialect = csv.Sniffer().sniff(csvfile.read())

For this module, the dialect class is a container class whose attributes contain information for how to handle delimiters (among other things like doublequotes, whitespaces, etc). You can check the delimiter attribute by using the following code:

print(dialect.delimiter)
# This will be either a comma or a semicolon, depending on what the input is

Therefore, in order to do a smart CSV reading, you could use something like the following:

if dialect.delimiter == ',':
df = pd.read_csv(input.csv) # Import the csv with a comma as the separator
elif dialect.delimiter == ';':
df = pd.read_csv(input.csv, sep=';') # Import the csv with a semicolon as the separator

More information can be found here.

pandas.read_csv not partitioning data at semicolon delimiter

Add quoting = 3. 3 is for QUOTE_NONE refer this.

   raw_data = pd.read_csv(filep,engine="python",index_col=False, header=None, delimiter=";", quoting = 3)

This will give [7 rows x 23 columns] dataframe

Read CSV file with semicolon as delimiter

If you're using semicolons (;) as your csv-file separator instead of commas (,), you can adjust that first line:

wine_data = pandas.read_csv('winequality-white-updated.csv', sep = ';', header = None)

The problem with your list comprehension is that [x.split(';') for x in wine_data_] iterates over the column names.

That being the case, you have no need for the line with the list comprehension. You can read in your data and be done.

wine_data = pandas.read_csv('winequality-white-updated.csv', sep = ',', header = None)
print (numpy.shape(wine_data))

Remove Semicolons as Line Delimiters Reading csv-file Using pandas.read_csv

If the semicolon is being used to separate lines, you can use the lineterminator parameter to correctly parse the file:

pd.read_csv(..., lineterminator=";")

Pandas CSV documentation



Related Topics



Leave a reply



Submit