我正在python中编写一个简短的脚本,它将扫描图像文件的文件夹列表,然后重新组织它们.
组织它们的一种可选方式是我们希望它们创建日期.
目前,我正在尝试按如下方式阅读图像创建日期
import os.path, time
f = open("hi.jpg")
data = f.read()
f.close()
print "last modified: %s" % time.ctime(os.path.getmtime(f))
print "created: %s" % time.ctime(os.path.getctime(f))
Run Code Online (Sandbox Code Playgroud)
但是我得到一个错误
Traceback (most recent call last):
File "TestEXIFread.py", line 6, in <module>
print "last modified: %s" % time.ctime(os.path.getmtime(f))
File "/usr/lib/python2.7/genericpath.py", line 54, in getmtime
return os.stat(filename).st_mtime
TypeError: coercing to Unicode: need string or buffer, file found
Run Code Online (Sandbox Code Playgroud)
谁能告诉我它意味着什么?
我已经阅读了我能找到的所有帖子,但我的情况似乎很独特。我对 Python 完全陌生,所以这可能是基础的。我收到以下错误:
UnicodeDecodeError:“charmap”编解码器无法解码位置 70 中的字节 0x8d:字符映射到未定义
当我运行代码时:
import csv
input_file = 'input.csv'
output_file = 'output.csv'
cols_to_remove = [4, 6, 8, 9, 10, 11,13, 14, 19, 20, 21, 22, 23, 24]
cols_to_remove = sorted(cols_to_remove, reverse=True)
row_count = 0 # Current amount of rows processed
with open(input_file, "r") as source:
reader = csv.reader(source)
with open(output_file, "w", newline='') as result:
writer = csv.writer(result)
for row in reader:
row_count += 1
print('\r{0}'.format(row_count), end='')
for col_index in cols_to_remove:
del row[col_index]
writer.writerow(row)
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我在同一目录中有两个文件:
阿拉伯语 WordNet 的 xml 数据库 ( http://nlp.lsi.upc.edu/awn/get_bd.php ) upc_db.xml
当我尝试运行 .py 文件以给出图像中的错误时,我试图检查 .py 文件是否正常工作,以便我可以将其导入为阿拉伯语单词的 WordNet
你能帮我解决这个问题吗?
谢谢
我正在我的计算机上加载一个 json 文件。我可以在不指定 Kaggle 编码的情况下加载它,不,错误。在我的电脑上,我收到标题中的错误。
with open('D:\soccer\statsbomb360\matches.json') as f:
data = json.load(f, encoding = 'utf8')
Run Code Online (Sandbox Code Playgroud)
添加errors = 'ignore'或更改编码为“拉丁”也不起作用。我有点不知道接下来要尝试什么,你能给我一个想法吗?
json 来自 statsbombs 免费提供的数据。有趣的是,在同一个数据集中,我有一些文件在 Kaggle/Colab 上给了我这个错误,但在我的电脑上却没有,但在那里指定encoding = 'latin'了这个技巧。
谢谢你!
即时通讯使用python3.3和sqlite3数据库.我有一个大约270mb的大文本文件,我可以用Windows7中的写字板打开.
该文件中的每一行如下所示:
term\t number \n
我想读取每一行并将值保存在数据库中.我的代码如下:
f = open('sorted.de.word.unigrams', "r")
for line in f:
#code
Run Code Online (Sandbox Code Playgroud)
我能够将所有数据读入我的数据库,但只是某一行,我建议可能是所有行的一半.然后我得到以下错误:
File "C:\projects\databtest.py", line 18, in <module>
for line in f:
File "c:\python33\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 140: character maps to <undefined>
Run Code Online (Sandbox Code Playgroud)
我试图用encoding = utf-8打开文件,但是其他编解码器都没有用.然后我尝试通过另存为utf-8 txt文件使用写字板进行复制.但是写字板崩溃了.
这里的问题在哪里,看起来python无法处理的那一行中有一些字符.我该怎么做才能完全读取我的文件?或者是否可以忽略此类错误消息并继续下一行?
您可以在此处下载打包文件:
http://wacky.sslmit.unibo.it/lib/exe/fetch.php?media=frequency_lists:sorted.de.word.unigrams.7z
非常感谢!
我有一个文件夹,里面有不同的子文件夹。我必须遍历所有文件并检查 John 和 Jose 的出现并分别替换为 Mikel 和 Mourinho。
这是我用 Python 编写的脚本。它工作正常但是当我遇到一个.gif文件时它给了我一个错误并且它没有进一步迭代。
你能告诉我为什么吗?
错误是
Traceback (most recent call last):
File "C:\Users\sid\Desktop\script.py", line 33, in <module>
os.chmod(path ,stat.S_IWRITE)
FileNotFoundError: [WinError 2] The system cannot find the file specified:'C:\Users\sid\Desktop\test\\images/ds_dataobject.gif.bak'
Run Code Online (Sandbox Code Playgroud)
我的代码:
import os,stat
import fileinput
import sys
rootdir ='C:\Users\spemmara\Desktop\test'
searchTerms={"John":"Mikel", "Jose":"Mourinho"}
def replaceAll(file,searchExp,replaceExp):
for line in fileinput.input(file, inplace=1):
if searchExp in line:
line = line.replace(searchExp,replaceExp)
sys.stdout.write(line)
for subdir, dirs, files in os.walk(rootdir):
for file in files:
path=subdir+'/'+file
print(path)
os.chmod(path ,stat.S_IWRITE)
for key,value …Run Code Online (Sandbox Code Playgroud) 我有一个 JSON 文件,其中包含超过 46k 条英语和其他语言的推文,我想将其保存为 csv 文件。以下是 json 文件的一部分。
\n\n [{"user_id": 938118866135343104, "date_time": "03/20/2018 18:38:35", "tweet_content": "RT @PTISPOfficial: \xd9\xbe\xd8\xa7\xda\xa9\xd8\xb3\xd8\xaa\xd8\xa7\xd9\x86 \xd8\xaa\xd8\xad\xd8\xb1\xdb\x8c\xda\xa9 \xd8\xa7\xd9\x86\xd8\xb5\xd8\xa7\xd9\x81 \xda\xa9\xdb\x92 \xd9\x88\xd8\xa7\xd8\xa6\xd8\xb3 \xda\x86\xdb\x8c\xd8\xa6\xd8\xb1\xd9\x85\xdb\x8c\xd9\x86 \xd8\xb4\xd8\xa7\xdb\x81 \xd9\x85\xd8\xad\xd9\x85\xd9\x88\xd8\xaf \xd9\x82\xd8\xb1\xdb\x8c\xd8\xb4\xdb\x8c \xd8\xa8\xd8\xba\xdb\x8c\xd8\xb1 \xda\xa9\xd8\xb3\xdb\x8c \xd9\xbe\xd8\xb1\xd9\x88\xd9\xb9\xd9\x88\xda\xa9\xd9\x88\xd9\x84 \xda\xa9\xdb\x92 \xd9\xbe\xd8\xa7\xda\xa9\xd8\xb3\xd8\xaa\xd8\xa7\xd9\x86 \xd8\xb3\xd9\xbe\xd8\xb1 \xd9\x84\xdb\x8c\xda\xaf \xda\xa9\xd8\xa7 \xd9\x85\xdb\x8c\xda\x86 \xd8\xaf\xdb\x8c\xda\xa9\xda\xbe\xd9\x86\xdb\x92 \xda\xa9\xdb\x92 \xd9\x84\xd8\xa6\xdb\x92 \xd8\xa7\xd8\xb3\xd9\xb9\xdb\x8c\xda\x88\xdb\x8c\xd9\x85 \xd9\x85\xe2\x80\xa6", "tweet_id": 976166125502427136}\n{"user_id": 959235642, "date_time": "03/20/2018 18:38:35", "tweet_content": "At last, Pakistan Have Witnessed The Most Thrilling Match Of Cricket In Pakistan, The Home. \\n\\n#PZvQG \\n#ABC", "tweet_id": 976166125535973378}\n{"user_id": 395163528, "date_time": "03/20/2018 18:38:35", "tweet_content": "RT @thePSLt20: SIX! 19.4 Liam Dawson to Anwar …Run Code Online (Sandbox Code Playgroud) python ×7
json ×2
python-3.x ×2
csv ×1
datecreated ×1
decode ×1
encoding ×1
exif ×1
file ×1
image ×1
pandas ×1
text-files ×1
utf-8 ×1
wordnet ×1
xml ×1