如何跳过没有内容的值?

Python初学者

如何将带有内容的值存储到字符串中?

我知道必须有一种更清洁、更有效的方法来做到这一点,但目前我正在努力寻找一种方法。我很感激对此有新的眼光,因为我一定错过了一些东西。我在这上面花了很奇怪的时间。

我的目标是:检查 sheet.values 是否有内容 -> 如果有,则存储为字符串

检查 sheet.values 是否有内容 -> 如果没有,跳过或不创建字符串

其优先级是 sheet.values 可以包含需要识别的未确定数量的内容。如 sheet.values 填入最多 [9] 一个实例但填入 [6] 另一个实例。所以它需要考虑到这一点。

sheet.values 也必须作为字符串返回,因为我稍后在代码中使用 makedirs() (它有点暴躁,如果你能提供帮助,这也需要工作)

我知道 for 循环应该能够帮助我,但只是还没有找到合适的循环。

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

Excel 文档显示如下:在此处输入图像描述

此脚本导致:index 9 is out of bounds for axis 0 with size 9这是真实预期的,因为 sheet.values 仅此金额。

谁能帮我?我知道这很乱

更新代码

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)
加里

似乎您正在尝试读取 csv 的第一列,并根据该值创建目录。

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)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章