How to ignore the first line of data when processing CSV data?
You could use an instance of the csv
module's Sniffer
class to deduce the format of a CSV file and detect whether a header row is present along with the built-in next()
function to skip over the first row only when necessary:
import csv
with open('all16.csv', 'r', newline='') as file:
has_header = csv.Sniffer().has_header(file.read(1024))
file.seek(0) # Rewind.
reader = csv.reader(file)
if has_header:
next(reader) # Skip header row.
column = 1
datatype = float
data = (datatype(row[column]) for row in reader)
least_value = min(data)
print(least_value)
Since datatype
and column
are hardcoded in your example, it would be slightly faster to process the row
like this:
data = (float(row[1]) for row in reader)
Note: the code above is for Python 3.x. For Python 2.x use the following line to open the file instead of what is shown:
with open('all16.csv', 'rb') as file:
Skipping first line in csv.file, when it's a string, with python
I humbly suggest to use pandas
to read CSV files. You can define the lines to skip and the format in a few lines:
import pandas as pd
# One single line to read all the data with the right format
df = pd.read_csv('C:\\Users\\Bruger\\Desktop\\dtu\\S\\data\\WL_geoid_values.txt',
skiprows = 1, # Skip first row
names = ['coordsx','coordsy','h_gravs'] # Rename each column
)
# Separating each column and turning then into lists
coordsx = df['coordsx'].tolist()
coordsy= df['coordsy'].tolist()
h_gravs= df['h_gravs'].tolist()
How to skip the first row when reading a csv file?
skip the first row when reading a csv file
For example,
package main
import (
"bufio"
"encoding/csv"
"fmt"
"io"
"os"
)
func readSample(rs io.ReadSeeker) ([][]string, error) {
// Skip first row (line)
row1, err := bufio.NewReader(rs).ReadSlice('\n')
if err != nil {
return nil, err
}
_, err = rs.Seek(int64(len(row1)), io.SeekStart)
if err != nil {
return nil, err
}
// Read remaining rows
r := csv.NewReader(rs)
rows, err := r.ReadAll()
if err != nil {
return nil, err
}
return rows, nil
}
func main() {
f, err := os.Open("sample.csv")
if err != nil {
panic(err)
}
defer f.Close()
rows, err := readSample(f)
if err != nil {
panic(err)
}
fmt.Println(rows)
}
Output:
$ cat sample.csv
one,two,three,four
1,2,3
4,5,6
$ go run sample.go
[[1 2 3] [4 5 6]]
$
$ cat sample.csv
PTN Ethernet-Port RMON Performance,PORT_BW_UTILIZATION,2019-06-29 20:00:00,33366
DeviceID,DeviceName,ResourceName,CollectionTime,GranularityPeriod,PORT_RX_BW_UTILIZATION,PORT_TX_BW_UTILIZATION,RXGOODFULLFRAMESPEED,TXGOODFULLFRAMESPEED,PORT_RX_BW_UTILIZATION_MAX,PORT_TX_BW_UTILIZATION_MAX
3174659,H1095,H1095-11-ISM6-1(to ZJBSC-V1),2019-06-29 20:00:00,15,22.08,4.59,,,30.13,6.98
3174659,H1095,H1095-14-ISM6-1(to T6147-V),2019-06-29 20:00:00,15,2.11,10.92,,,4.43,22.45
$ go run sample.go
[[DeviceID DeviceName ResourceName CollectionTime GranularityPeriod PORT_RX_BW_UTILIZATION PORT_TX_BW_UTILIZATION RXGOODFULLFRAMESPEED TXGOODFULLFRAMESPEED PORT_RX_BW_UTILIZATION_MAX PORT_TX_BW_UTILIZATION_MAX] [3174659 H1095 H1095-11-ISM6-1(to ZJBSC-V1) 2019-06-29 20:00:00 15 22.08 4.59 30.13 6.98] [3174659 H1095 H1095-14-ISM6-1(to T6147-V) 2019-06-29 20:00:00 15 2.11 10.92 4.43 22.45]]
$
Skip first line(field) in loop using CSV file?
The best way of doing this is skipping the header after passing the file object to the csv
module:
with open('myfile.csv', 'r', newline='') as in_file:
reader = csv.reader(in_file)
# skip header
next(reader)
for row in reader:
# handle parsed row
This handles multiline CSV headers correctly.
Older answer:
Probably you want something like:
firstline = True
for row in kidfile:
if firstline: #skip first line
firstline = False
continue
# parse the line
An other way to achive the same result is calling readline
before the loop:
kidfile.readline() # skip the first line
for row in kidfile:
#parse the line
How to skip the headers when processing a csv file using Python?
Your reader
variable is an iterable, by looping over it you retrieve the rows.
To make it skip one item before your loop, simply call next(reader, None)
and ignore the return value.
You can also simplify your code a little; use the opened files as context managers to have them closed automatically:
with open("tmob_notcleaned.csv", "rb") as infile, open("tmob_cleaned.csv", "wb") as outfile:
reader = csv.reader(infile)
next(reader, None) # skip the headers
writer = csv.writer(outfile)
for row in reader:
# process each row
writer.writerow(row)
# no need to close, the files are closed automatically when you get to this point.
If you wanted to write the header to the output file unprocessed, that's easy too, pass the output of next()
to writer.writerow()
:
headers = next(reader, None) # returns the headers or `None` if the input is empty
if headers:
writer.writerow(headers)
How do i skip the first two lines from my text file?
def file_search():
userInput = input('Enter a country: ').lower()
result = []
with open("json.txt", 'r') as f:
for x in f:
if userInput in x.lower():
result.append(x.split(';'))
for s in result:
print(s[1] + "check:" + s[3])
file_search()
from collections import Counter
counter = Counter()
with open('json.txt') as f:
for i in range(0,2):
next(f)
for line in f:
splits = line.split(';')
change = float(splits[6])
country = splits[1].strip()
counter[country] += change
#Percentage Change By Countries"
print()
print ("Percentage Change By Countries")
for country, change_sum in counter.most_common():
print(country, change_sum,"%")
Skip specific rows using read.csv in R
One way to do this is using two read.csv
commands, the first one reads the headers and the second one the data:
headers = read.csv(file, skip = 1, header = F, nrows = 1, as.is = T)
df = read.csv(file, skip = 3, header = F)
colnames(df)= headers
I've created the following text file to test this:
do not read
a,b,c
previous line are headers
1,2,3
4,5,6
The result is:
> df
a b c
1 1 2 3
2 4 5 6
Java Reading CSV file into array but ignore first line
Add a readLine()
before the while loop to skip the first line.
br = new BufferedReader(new FileReader(file));
br.readLine(); //read the first line and throw it away
while ((line = br.readLine()) != null) {
Related Topics
Apply Function to Each Element of a List
Integer Division in Python 2 and Python 3
Getting an "Invalid Syntax" When Trying to Perform String Interpolation
How to Subtract a Day from a Date
How to Create a Zip Archive of a Directory
How to Extract Text from a PDF File
Element-Wise Addition of 2 Lists
How to Round to 2 Decimals with Python
Create Multiple Dataframes in Loop
Sorting a Python List by Two Fields
Import CSV File as a Pandas Dataframe
How to Parse Dates with -0400 Timezone String in Python
How to Use 'Return' to Get Back Multiple Values from a for Loop? How to Put Them in a List
Behavior of Exec Function in Python 2 and Python 3
Freezing/Hanging Tkinter Gui in Waiting for the Thread to Complete