简体   繁体   English

与文本文件交互时,Python 3中str.split()的替代方法

[英]Alternatives to str.split() in Python 3 when interacting with a text file

I'm currently playing around with making a file duplicator in Python. 我目前正在使用Python创建文件复制器。 It currently works on its own when I customise the number of duplications in the actual program, but I am working on making a separate and basic setup file. 当我在实际程序中自定义重复项的数量时,它当前可以单独工作,但是我正在制作一个单独的基本安装文件。 This will allow you to specify the number of duplicates you want. 这将允许您指定所需的重复次数。

At the moment, this number is saved onto a plain text document in list form. 目前,此号码以列表形式保存到纯文本文档中。 So the document can look like [0, 1, 2, 3] to [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] depending on what you put in. 因此,文档的外观可能从[0,1,2,3]到[0,1,2,3,4,5,6,7,7,8,9,10,11,12],具体取决于您输入的内容。

The problem is that when the actual duplicator program tries to get the list from the plain text file, it interprets it as a string, so it looks like 问题是,当实际的复制程序尝试从纯文本文件中获取列表时,它将其解释为字符串,因此看起来

count = ['[0, 1, 2, 3, 4, 5]']

I have searched the internet far and wide for a solution to this, but the only solution I can find is to use str.split(). 我已经在互联网上四处搜寻有关此问题的解决方案,但是我唯一能找到的解决方案是使用str.split()。 From my knowledge, this was removed in Python 3. Are there any alternatives that I can employ in this situation? 据我所知,这在Python 3中已被删除。

Here is the code for the actual duplicator: 这是实际复印机的代码:

#!/usr/bin/env python
import shutil
import getpass
data = open("/Volumes/USB/Data.txt", "r")
username = getpass.getuser()
count = data.readlines(1)
print(str(count))
for x in count:
    shutil.copy2('/Volumes/USB/img.jpg', 'img{0}.jpg'.format(x))
    print(str(count))

And here is the code for the setup file: 这是安装文件的代码:

import time
import fileinput
with open("/Volumes/USB/Data,txt", "r") as f:
# Precount is where the original list will go to let them know how many have previously been selected. I will convert this to the number of entries in the list eventually.
    precount = list(f.readlines(1))
    print('Enter the desired amount of images. Your current amount is {0}.'.format(precount))
countbase = input()
count = [0]
n = 1
while int(n) < int(countbase):
    list.append(count, n)
    n += 1
with open("/Volumes/USB/Data.txt", "w") as f:
    f.write(str((count)))
print('Configuration complete!')
time.sleep(1)
quit()

Thanks for any help! 谢谢你的帮助!

You would probably be happier with next(data) , but what you have currently is a very short list returned by .readlines(1) . 您可能对next(data)会更满意,但是当前拥有的是.readlines(1)返回的简短列表。

If you take the initial list element you'll just have a string: 如果使用初始列表元素,则只有一个字符串:

count[0]

You can pass that to a JSON parser to turn the string into a list of integers: 您可以将其传递给JSON解析器,以将字符串转换为整数列表:

import json
counts = json.loads(count[0])

Then you can iterate over those int s to your heart's content. 然后,把这些迭代int s到你的心脏的内容。

There are a few ways to do this. 有几种方法可以做到这一点。

The first, and simplest, would be to use the ast module to parse the input: 第一个也是最简单的方法是使用ast模块来解析输入:

import ast

ast.literal_eval('[1, 2, 3, 4, 5]')

This produces the list object [1, 2, 3, 4, 5] . 这将产生list对象[1, 2, 3, 4, 5]

If you foresee adding more options, it would probably be better to store your configuration data in JSON format. 如果您预计会添加更多选项,则最好以JSON格式存储配置数据。 You can use the functions in the json module for that (in particular, json.dump and json.load ). 您可以为此使用json模块中的函数(特别是json.dumpjson.load )。

Lastly, you could use the pickle module to save your configuration in binary format. 最后,您可以使用pickle模块将配置保存为二进制格式。 I wouldn't recommend this, because pickle allows arbitrary code execution. 我不建议这样做,因为pickle允许执行任意代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM