扫描当前文件夹中的所有.CSV,并根据上一个列字符串添加新列以后填充

发布于 2025-02-05 17:53:58 字数 1843 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黑白记忆 2025-02-12 17:53:59

如果 URL列格式是统一的:在'/'上拆分,然后采取第三个项目;将其分配在'。'上,然后采取第一个项目。

s = '''https://safariland.shopdomain.com/fghj/shoes'''
_,_,z,*_ = s.split('/')
whatiwant,*_ = z.split('.')
print(z)
print(whatiwant)

>>>
safariland.shopdomain.com
safariland

If the url column format is uniform: Split on '/' and take the third item; split that on '.' and take the first item.

s = '''https://safariland.shopdomain.com/fghj/shoes'''
_,_,z,*_ = s.split('/')
whatiwant,*_ = z.split('.')
print(z)
print(whatiwant)

>>>
safariland.shopdomain.com
safariland
晨曦慕雪 2025-02-12 17:53:59
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
rem The following settings for the source directory & destination directory are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files\t w o\subdir3"
SET "destdir=u:\your results"

for /f "delims=" %%G in ('dir /b /a-d "%sourcedir%\*.csv"') do (
 rem filename in %%G
 SET "header=Y"
 FOR /f "usebackqdelims=" %%e IN ("%sourcedir%\%%G") DO (
  FOR /f "tokens=2delims=/." %%q IN ("%%e") DO ECHO %%e,%%q&SET "header="
  IF DEFINED header ECHO %%e,"columnheader4"
 )
)>"%destdir%\tempfile"& MOVE /y "%destdir%\tempfile" "%sourcedir%\%%G" >NUL
GOTO :EOF

在应用真实数据之前,请务必针对测试目录进行验证。

假设您的.CSV文件具有标头行,并且每个数据行都遵循您指示的模式,请进行逗号分隔(请进行测试的原始数据),则

dir在目录中找到所有.csv文件。在转动。下一个针对的然后在%% e中分析该行,并将%% q设置为// 在之前(假设/仅出现在第3列中)。 echo仅在分配%% Q时执行,因此不在标题行上(s),header将设置为< em>什么都没有,并且变得不确定。

如果找不到数据线,则该行必须是标头,因此使用第四列标题输出该行。

最后,将数据收集到临时文件并移至原始文件。

我会再说一遍,

在应用于实际数据之前,请始终验证测试目录。

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
rem The following settings for the source directory & destination directory are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files\t w o\subdir3"
SET "destdir=u:\your results"

for /f "delims=" %%G in ('dir /b /a-d "%sourcedir%\*.csv"') do (
 rem filename in %%G
 SET "header=Y"
 FOR /f "usebackqdelims=" %%e IN ("%sourcedir%\%%G") DO (
  FOR /f "tokens=2delims=/." %%q IN ("%%e") DO ECHO %%e,%%q&SET "header="
  IF DEFINED header ECHO %%e,"columnheader4"
 )
)>"%destdir%\tempfile"& MOVE /y "%destdir%\tempfile" "%sourcedir%\%%G" >NUL
GOTO :EOF

Always verify against a test directory before applying to real data.

Assuming your .csv files have a header line and each data line follows the pattern you indicate, comma-separated (raw data for testing, please) then

The dir finds all of the .csv files in the directory. header is set to y to indicate that a header line is expected, and then each line of the specified file is assigned to %%e in turn. the next for then analyses the line in %%e and sets %%q to the string after the // and before the . (assuming that / and . only occur in column3). The echo will only be executed if %%q is assigned, so not on header line(s), and header will be set to nothing and become undefined.

If no data line has been found, then the line must be a header, so output that line with the fourth column header.

Finally, the data is gathered to a temporary file and moved over the original.

I'll say again,

Always verify against a test directory before applying to real data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文