Home > Mobile >  How do I skip values with no content?
How do I skip values with no content?

Time:10-03

How do I store the values with content into strings?

I know there has to be a much cleaner and more efficient way of doing this but currently I am struggling to find a way. I would appreciate a set of fresh eyes on this since I must be missing something. I have spent an outlandish time on this.

My objective is: Check if sheet.values has content -> if so, store as a string

Check if sheet.values has content -> if not, skip or create no string

The priority of this is that sheet.values can contain an undetermined amount of content that needs to be identified. Such as sheet.values filled in being up to [9] one instance but being filled in to [6] another instance. So it needs to account for this.

The sheet.values also have to return as a string as I use makedirs() later in the code (it gets a bit testy this also needs work if you can help)

I know a for loop should be able to help me but just not found the right one just yet.

import os
import pandas as pd
from openpyxl import load_workbook
from pandas.core.indexes.base import Index

os. chdir("C:\\Users\\NAME\\desktop")
workbook = pd.ExcelFile('Example.xlsx')

sheet = workbook.parse('Sheet1')

print (sheet.values[0])

os.getcwd()
path = os.getcwd()


for input in sheet.values:
  if any(sheet.values): 
    if input == None:
      break

else:
  if any(sheet.values): 
    sheet.values == input
    set

str1 = '1'.join(sheet.values[0])
str2 = '2'.join(sheet.values[1])
str3 = '3'.join(sheet.values[2])
str4 = '4'.join(sheet.values[3])
str5 = '5'.join(sheet.values[4])
str6 = '6'.join(sheet.values[5])
str7 = '7'.join(sheet.values[6])
str8 = '8'.join(sheet.values[7])
str9 = '9'.join(sheet.values[8])
str10 = '10'.join(sheet.values[9])
str11 = '11'.join(sheet.values[10])
str12 = '12'.join(sheet.values[11])
str13 = '13'.join(sheet.values[12])
str14 = '14'.join(sheet.values[13])
str15 = '15'.join(sheet.values[14])
str16 = '16'.join(sheet.values[15])
str17 = '17'.join(sheet.values[16])
str18 = '18'.join(sheet.values[17])
str19 = '19'.join(sheet.values[18])
str20 = '20'.join(sheet.values[19])
str21 = '21'.join(sheet.values[20])

########################ONE################################################

try:

  if not os.path.exists(str1):

        os.makedirs(str1)

except OSError:
   print ("Creation of the directory %s failed" % str1)
else:
  print ("Successfully created the directory %s " % str1)

########################TWO################################################

try:
  if not os.path.exists(str2):

        os.makedirs(str2)

except OSError:
   print ("Creation of the directory %s failed" % str2)
else:
  print ("Successfully created the directory %s " % str2)


########################THREE################################################


try: 
  if not os.path.exists(str3):

        os.makedirs(str3)
except OSError:
   print ("Creation of the directory %s failed" % str3)
else:
  print ("Successfully created the directory %s " % str3)

########################FOUR################################################

try: 
  if not os.path.exists(str4):

        os.makedirs(str4)
except OSError:
   print ("Creation of the directory %s failed" % str4)
else:
  print ("Successfully created the directory %s " % str4)

Note: The makedirs() code runs down till to the full amount of strings

The Excel document shows the following: enter image description here

This script results in: index 9 is out of bounds for axis 0 with size 9 This is truthfully expected as the sheet.values only this amount.

Can anyone help me? I know it is messy

Updated Code

import os
import pandas as pd
from openpyxl import load_workbook
from pandas.core.indexes.base import Index

os. chdir("C:\\Users\\NAME\\desktop")
workbook = pd.ExcelFile('Example.xlsx')

sheet = workbook.parse('Sheet1')

print (sheet.values[0])

os.getcwd()
path = os.getcwd()

print ("The current working Directory is %s" % path)

for col in sheet.values:
    for row in range(len(col)):
        dir_name = str(row   1)   col[row]
try:
        os.makedirs(dir_name, exist_ok=True)

except OSError:
   print ("Creation of the directory %s failed" % dir_name)
else:
  print ("Successfully created the directory %s " % dir_name)

This new updated version of code now creates a single folder in the excel document using the Photos\Greece\Rhodes\Day4\Family path at the end.

The following is printed. Successfully created the directory Photos\Greece\Rhodes\Day4\Family

'row' also seems to represent '0' as a value and the '1' is only adding to the value than cycling through the rows.

Can someone help or explain why this is happening?

CodePudding user response:

it seems like you're trying to read the first column of a csv, and create directories based on the value.

with open(mypath file) as file_name:
    file_read = csv.reader(file_name)
    file = list(file_read)

for col in file:
    for row in range(len(col)):
        dir_name = str(row   1)   col[row]
        try:
            # https://docs.python.org/3/library/os.html#os.makedirs
            os.makedirs(dir_name, exist_ok=True)
        except OSError:
            print ("Creation of the directory %s failed" % str1)
        else:
            print ("Successfully created the directory %s " % str1)
  • Related