使用 Python 修改目录中的所有文件(循环)

Modify all files in directory using Python (Loop)

我在为我的脚本制作循环时遇到了很多麻烦。最初该脚本正在修改一个 CSV 文件,但我对其进行了编辑以循环遍历一个文件夹“CSVtoGD”并修改该文件夹中的每个 csv。到目前为止没有运气。修改后的脚本为:

from pathlib import Path 
import pandas as pd
dir = r'/users/krzysztofpaszta/CSVtoGD' 
csv_files = [f for f in Path(dir).glob('*.csv')] 



for csv in csv_files: #iterate list
   
    df = pd.read_csv(csv, sep=": \s+", engine='python', names=['dane', 'wartosc'])
    # creating columns with names: ścieżka_do_pliku:czcionka.ttf 
    df['dana_czcionka'] = df['dane'].str.split(':').str[0]

    print('\n--- df ---\n')
    print(df.to_string())

    with open('csv', 'w') as f_out:
        writer = csv.writer(f_out)
    
# sorting data by columns: ścieżka_do_pliku:czcionka.ttf 
        for name, data in df.groupby('dana_czcionka'):
            print('\n---', name, '---\n')
        
            headers = (data['dane'] + ":").to_list()
            print(headers)
    
            values = data['wartosc'].to_list()
            print(values)
            values.insert(0, name) # - Adding name (path) to every row
            values.insert(0, name)
            #writer.writerow(headers) 
            writer.writerow(values)
            
# showing results in terminal, saving to file

    print(f'{csv.name} saved.')

我收到此错误:

AttributeError                            Traceback (most recent call last)
/var/folders/zw/12ns4dw96zb34ktc_vfn0zp80000gp/T/ipykernel_49714/1288759270.py in <module>
     16 
     17     with open('csv', 'w') as f_out:
---> 18         writer = csv.writer(f_out)
     19 
     20 # grupowanie danych według kolumn ścieżka_do_pliku:czcionka.ttf

AttributeError: 'PosixPath' object has no attribute 'writer'

我试图通过修改 'writer' 来实现这一点,但我想我现在的知识还很少。我认为一个简单的循环会得到结果,但我不知道是我的循环构建错误还是问题所在..

没有循环的原始脚本运行正常。原始脚本(修改一个 csv)如下所示:

import pandas as pd
import csv
df = pd.read_csv('TTF-Projects-INFO.csv', sep=": \s+", engine='python', names=['dane', 'wartosc'])

# creating columns with names like: ścieżka_do_pliku:czcionka.ttf 
df['dana_czcionka'] = df['dane'].str.split(':').str[0]

print('\n--- df ---\n')
print(df.to_string())

with open('newTTF-Projects-INFO.csv', 'w') as f_out:
    writer = csv.writer(f_out)
    
# sorting data by columns: ścieżka_do_pliku:czcionka.ttf 
    for name, data in df.groupby('dana_czcionka'):
        print('\n---', name, '---\n')
        
        headers = (data['dane'] + ":").to_list()
        print(headers)
    
        values = data['wartosc'].to_list()
        print(values)
        values.insert(0, name) # - add name (path) to every row with data
        #writer.writerow(headers) 
        writer.writerow(values)
            
# showing effect in terminal, saving to file

print('\n--- file ---\n')
print(open('newTTF-Projects-INFO.csv').read())

您的问题是名称冲突。原始代码使用 import csv 导入库。但是您还使用 csv 作为循环中的变量

for csv in csv_files: #iterate list

所以把你的变量名改成这样的:

for csv_file in csv_files: #iterate list

以及以下变量的出现:

...
for csv_file in csv_files: #iterate list
    df = pd.read_csv(csv_file, sep=": \s+", engine='python', names=['dane', 'wartosc'])
    # creating columns with names: ścieżka_do_pliku:czcionka.ttf 
    df['dana_czcionka'] = df['dane'].str.split(':').str[0]

    print('\n--- df ---\n')
    print(df.to_string())

    with open(csv_file, 'w') as f_out:
        writer = csv.writer(f_out)
...
print(f'{csv_file} saved.')

最后,在开头添加行import csv
现在您的代码应该可以正常工作了。


或者您可以使用行

import csv as cs

并将作者行更改为

writer = cs.writer(f_out)

在这种情况下,您可以保留 csv 变量名。
但是恕我直言,这在命名约定方面不太清楚。