pythoncsvencodingcharacter-encoding

Python: Reading a CSV file with different encryptions but bypassing first few rows


I have a CSV file where the first 8 rows explain the file and the data starts on row 10. I tried skipping the data first, however, it gave me errors with reading the file:

I included the names=range(20) because it kept expecting a smaller range.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import os # Check path
import csv
import glob

# Check path
current_path = os.getcwd()
print(current_path)
data = pd.read_csv('Events.csv', encoding="utf-8", names = range (20))

Output: The output was 's' for row 1 col 1, and 'NaN' for the rest of the dataframe, as 'utf-8' is the encoding for the top portion and 'cp1252' is the encoding for the data itself.

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 0 s NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 3 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 4 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN

When I open the csv file and delete the first few rows, I am able to read the file. I don't want to open the file and delete the first few rows to make this code work, as I want this code to work for similar csv's that I provide it (the file changes depending on what time it is as it stores events).

Is there a way to get past the first few rows and see my data?


Solution

  • Try

    data = pd.read_csv('Events.csv', encoding="utf-8", skiprows=10)