OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

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

  • Thread starter Thread starter Michelle Bogush
  • Start date Start date
M

Michelle Bogush

Guest
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.

Code:
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?
<p>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:</p>
<p>I included the names=range(20) because it kept expecting a smaller range.</p>
<pre><code>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))
</code></pre>
<p>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.</p>
<p>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</p>
<p>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).</p>
<p>Is there a way to get past the first few rows and see my data?</p>
 
Top