AnswerBun.com

How to transport data from csv to Excel

I’ve got 2 files.

file1:

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

file2:

0,1,2,3,4,5
0,1,2,3,4,5,6,7,8,9,7585,10,11,12,14,15,16,17,18
0,1,2,3,4,5,6,7,8,9,7585,10,11,12,13,14,15,16,17,18,19,20,21,22
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14

Im trying to export all the rows where index 10 == 7585 to Excel,
regardless of the lenght of the rows.

This is my code:

my_path = r'c:dataEKLDesktopmy-files' 

for file in os.listdir(my_path):
    path_file = os.path.join(my_path, file)
    with open(path_file, 'r') as output:
        reader = csv.reader(output, delimiter = ',')
        read = [row for row in reader if row] 
        for row in read:                  
                if row[10] == '7585':
                    print(','.join(row[0:]))

This is my outcome:

Traceback (most recent call last):

  File "C:dataEKLDesktopPythonPythongMySQLuntitled2.py", line 14, in <module>
    if row[10] == '7585':

IndexError: list index out of range

This is my desired outcome in an EXCEL worksheet:

0,1,2,3,4,5,6,7,8,9,7585,10,11
0,1,2,3,4,5,6,7,8,9,7585,10,11,12,14,15
0,1,2,3,4,5,6,7,8,9,7585,10,11,12,13,14,15,16,17,18,19,20
0,1,2,3,4,5,6,7,8,9,7585,10,11,12,14,15,16,17,18
0,1,2,3,4,5,6,7,8,9,7585,10,11,12,13,14,15,16,17,18,19,20,21,22

Stack Overflow Asked by TangerCity on February 19, 2021

2 Answers

2 Answers

This worked for me:

import os
import csv

my_path = r'sof' # local path of csv files
index = 10 # index you would like to check
value = '7585' # value of the index it should be

for file in os.listdir(my_path):
    path_file = os.path.join(my_path, file)
    with open(path_file, 'r') as output:
        reader = csv.reader(output, delimiter = ',')
        read = [row for row in reader if row] 
        for row in read:               
            if len(row) >= index and row[index] == value:
                print(','.join(row[0:]))

Output

0,1,2,3,4,5,6,7,8,9,7585,10,11
0,1,2,3,4,5,6,7,8,9,7585,10,11,12,14,15
0,1,2,3,4,5,6,7,8,9,7585,10,11,12,13,14,15,16,17,18,19,20
0,1,2,3,4,5,6,7,8,9,7585,10,11,12,14,15,16,17,18
0,1,2,3,4,5,6,7,8,9,7585,10,11,12,13,14,15,16,17,18,19,20,21,22

Update - with Excel Export

import os
import csv
import xlsxwriter

my_path = r'sof' 
index = 10
value = '7585'

# Create a workbook and add a worksheet.
workbook = xlsxwriter.Workbook('test.xlsx')
worksheet = workbook.add_worksheet()

# Start from the first row
rown = 0

for file in os.listdir(my_path):
    path_file = os.path.join(my_path, file)
    with open(path_file, 'r') as output:
        reader = csv.reader(output, delimiter = ',')
        read = [row for row in reader if row] 
        for row in read:               
            if len(row) >= index and row[index] == value:
                worksheet.write_row("A"+str(rown+1),row)
                rown+=1

workbook.close()

Correct answer by Stefan Schulz on February 19, 2021

Didn't check if it's running, but I'd try something like this:

my_path = r'c:dataEKLDesktopmy-files' 

for file in os.listdir(my_path):
    path_file = os.path.join(my_path, file)
    with open(path_file, 'r') as output:
        reader = csv.reader(output, delimiter = ',')
        read = [row for row in reader if row] 
        for row in read:                  
                if (len(row) > 10) and (row[10] == '7585'):  # add extra condition
                    print(','.join(row[0:]))

Answered by zoldseges on February 19, 2021

Add your own answers!

Related Questions

Why are declarations in a switch construct legal?

1  Asked on November 12, 2021 by l-s-roth

 

How do I import InvalidURL exception in praw for python?

1  Asked on November 12, 2021 by johannes-pertl

   

Place button on bottom of a div

7  Asked on November 12, 2021 by jackal

       

Javascript: using tuples as dictionary keys

4  Asked on November 12, 2021 by hasen

     

Gmail REST api batch support for getting messages

3  Asked on November 12, 2021 by dune76

   

Custom Route Doesn’t Get The Query String

2  Asked on November 12, 2021 by at-2017

       

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved.