所以我在R中有一个数据框,它包含整数,NA和单元格内随机分组的字符串.每个单元只有一种数据类型.我想知道的是如何将包含字符串的所有单元格更改为NA.知道我怎么能这样做吗?
我有一个字符串列表,所有字符串都遵循名称的部分格式除以下划线.这是格式:
string="somethingX_somethingY_one_two"
我想知道怎么做从列表中的每个字符串中提取"one_two"并重建列表,以便每个条目只有"somethingX_somethingY".我知道在C中,有一个strtok函数可用于拆分标记,但我不确定是否有类似的方法或策略在Python中获得相同的效果.请帮帮我?
我不确定为什么这个Pickle示例没有显示两个字典定义.据我所知,"ab +"应该意味着pickle.dat文件被附加到并且可以从中读取.我是整个泡菜概念的新手,但网上的教程似乎不仅仅是初始存储.
import cPickle as pickle
def append_object(d, fname):
"""appends a pickle dump of d to fname"""
print "append_hash", d, fname
with open(fname, 'ab') as pickler:
pickle.dump(d, pickler)
db_file = 'pickle.dat'
cartoon = {}
cartoon['Mouse'] = 'Mickey'
append_object(cartoon, db_file)
cartoon = {}
cartoon['Bird'] = 'Tweety'
append_object(cartoon, db_file)
print 'loading from pickler'
with open(db_file, 'rb') as pickler:
cartoon = pickle.load(pickler)
print 'loaded', cartoon
Run Code Online (Sandbox Code Playgroud)
理想情况下,我希望使用for循环构建一个字典,然后将key:value对添加到pickle.dat文件,然后清除字典以节省一些RAM.
这里发生了什么?
我正在尝试删除我正在运行我的Python程序的目录中的某个文件.
def erase_custom_file():
directory=os.listdir(os.getcwd())
for somefile in directory:
if somefile=="file.csv":
os.remove(???)
Run Code Online (Sandbox Code Playgroud)
我不确定我的下一步应该是什么.我知道这os.remove需要一个参数的路径,但我不知道如何将它指向我想要的文件.请帮帮我?
我有这个代码:
filenames=["file1","FILE2","file3","fiLe4"]
def alignfilenames():
#build a string that can be used to add labels to the R variables.
#format goal: suffixes=c(".fileA",".fileB")
filestring='suffixes=c(".'
for filename in filenames:
filestring=filestring+str(filename)+'",".'
print filestring[:-3]
#now delete the extra characters
filestring=filestring[-1:-4]
filestring=filestring+')'
print "New String"
print str(filestring)
alignfilenames()
Run Code Online (Sandbox Code Playgroud)
我试图让字符串变量看起来像这种格式:suffixes=c(".fileA",".fileB".....)但添加最后的括号是行不通的.当我按原样运行此代码时,我得到:
suffixes=c(".file1",".FILE2",".file3",".fiLe4"
New String
)
Run Code Online (Sandbox Code Playgroud)
知道发生了什么或如何解决它?
我无法让我的程序工作.基本上,我从两个文件读入,然后将数据写入一个.任何人都可以指出问题是什么?我一直在尝试写入文件的print OUT语句中获得有关实例化的错误.这是我的代码:
print "Please input file name \n";
$infile=<DATA>;
$infile2=<DATA>;
open IN, "$infile";
open JUNK, "$infile2";
open OUT, '>' ,'convertedback.txt';
$line = <IN>;
$line2 = <JUNK>;
#pull first line from sample (name 1)
print OUT "$line";
$line =<IN>;
#pull sequence line from FASTQ
print OUT "$line";
#pull line from quality file *2
print OUT "$line2";
$line2 =<JUNK>;
print OUT "$line2";
#Repeat until EOF
while($line =<IN>) {#for lines 5 to end
#Build Line 1
print "line 1 inf (name) is\n";
print …Run Code Online (Sandbox Code Playgroud) 我有一个遵循格式的文件
_line 1
this is a string on a line
_line 2
this is another string
_line 3
short line
Run Code Online (Sandbox Code Playgroud)
我正在尝试编写一些Python代码,以获取其下面字符串长度最长的字符串的_line X标签.你能帮我修改一下我的代码吗?这是我到目前为止所拥有的.
f = open('test.txt', 'r')
print f
read="null"
top_read_line_length="0"
topreadline="null"
for line in f:
checkifread=line.find('line')
if checkifread==1:
print "Read label found"
#means we are on a read line
currentread=line
else:
#We are on a sequence line for currentread.
currentlength=len(line)
print currentlength
print top_read_line_length
if int(top_read_line_length) < int(currentlength):
print topreadline
topreadline=currentread#now topreadline label is the "_line" string
topreadlinelength=int(currentlength)
print topreadline …Run Code Online (Sandbox Code Playgroud)