Answer my own question – share your knowledge, Q&A-style
我使用CSV文件作为Dataframe,但它在桌面上的一个受密码保护的RAR文件中。
由于每个用户都有不同的名称,生成到桌面的不同路径,因此无论用户名如何,我也会自动生成路径,这样我就可以在Windows的任何计算机上使用路径,而不必更改路径字符串。
这将是我今天对StackOverflow社区的贡献!
注意:请随时发布改进的模型以满足这一需求。
发布于 2022-05-29 04:36:20
from rarfile import RarFile
from getpass import getpass
import pandas as pd
import os
desktop = os.environ['USERPROFILE'] + '\Desktop' # create desktop path
rar_files = RarFile(desktop + '\archive_rar.rar', 'r') # open RAR file
specific_file = rar_files.open('file_inside_rar.csv',pwd=getpass()) # open csv file that is inside RAR
print('\033[A\033[A') # Clear row of the password request in the terminal
df = pd.read_csv(specific_file) # Dataframe created from csv file
print(df.iloc[0].values[0]) # row 0, value 0
print(df.iloc[1].values[0]) # row 1, value 0
print(df.iloc[2].values[0]) # row 2, value 0
终端中的字符串将如下所示:
getpass()
将向您询问RAR文件的密码:
Password:
您将键入,但密码文本将不会出现!
输入完密码后,按ENTER键。
然后print('\033[A\033[A')
将清除终端中的现有文本:
然后第一次print
row_1_value
然后是第二个print
row_1_value
row_2_value
然后是第三个print
row_1_value
row_2_value
row_3_value
https://stackoverflow.com/questions/72420943
复制相似问题